Click here to Skip to main content
16,014,294 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hai dudes,
i m doing project in c# with windows applications for searching files from local drives and im done but while running my project i search my files it shown on listbox......but i cant open the files directly from listbox....for eg:if i search .doc files from local drive D: means it list all the files from that drive but if i open from list box i cant....so please help how to do this...
This is my code
C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            cboDirectory.Items.Clear();
            foreach (string s in Directory.GetLogicalDrives())
            {
                cboDirectory.Items.Add(s);
            }
            cboDirectory.Text = "C:\\";
        }

        void DirSearch(string sDir)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d, txtFile.Text))
                    {
                        lstFilesFound.Items.Add(f);
                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lstFilesFound.Items.Clear();
            txtFile.Enabled = false;
            cboDirectory.Enabled = false;
            btnSearch.Text = "Searching...";
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();
            DirSearch(cboDirectory.Text);
            btnSearch.Text = "Search";
            this.Cursor = Cursors.Default;
            txtFile.Enabled = true;
            cboDirectory.Enabled = true;
        }
Posted
Updated 7-Aug-13 0:41am
v2
Comments
Maarten Kools 7-Aug-13 7:12am    
Try the answer suggest here[^]

Do not use DoEvents! Instead, execute your search in a separate thread.

Now, the real problem is trying to update UI with intermediate search results (already found items) when the search is not yet complete. If you do so, you should understand that you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Hey,

Try this.
using System.Diagnostics;

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal ;
startInfo.FileName = "c://";            
process.StartInfo = startInfo;
Process.Start();


In above code try to modify

startInfo.FileName = lstFilesFound.selectedValue;  


This will help u to open ur folder and different types of Files.

Please rate ,if it helps.

Regards,
Kajal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900