0

I write this code and work when I select any folder (with search option = SearchOption.AllDirectories ) but for

Drive Like D:\ I get error

" access to path D:\System Volume Information is denied"

and I add "\" to this path but still get error

        if (dirListBox.Items.Count == 0)
        {
            foreach (int Index in disksListBox.CheckedIndices)
            {
                String Dir = disksListBox.Items[Index].ToString().Substring(0, 2);
                Dir += @"\";
                if (CheckExists(Dir))
                {
                    Dirs.Add(Dir);
                }
            }
        }
        else
        {
            for (int Index = 0; Index < dirListBox.Items.Count; Index++)
            {
                String Dir = dirListBox.Items[Index].ToString();
                Dirs.Add(Dir);
             }
        }
        if (rdb_thisdir.Checked == true)
            OptionDir = SearchOption.TopDirectoryOnly; 
        else
            OptionDir = SearchOption.AllDirectories; // when search D:\ , Get Error But Work for Folder

        if (rdbversion1.Checked == true)
        {
            ListViewItem lstitm = new ListViewItem();
            foreach (String Dir in Dirs)
            {
                try
                {
                    DirectoryInfo DirInfo = new DirectoryInfo(Dir);
                    FileInfo[] FileS = DirInfo.GetFiles(SearchPattern,OptionDir); //error when Dir="D:\\"

                    foreach (FileInfo file in FileS)
                    {
                        try
                        {
                            if (Check_Attributes(file) && Check_DateTime(file))
                            {
                                listFileFounded.Items.Add(file.FullName.ToString());
                                lstitm = lwfound.Items.Add(file.Extension.ToString());
                                lstitm.SubItems.Add(file.Name.ToString());
                                lstitm.SubItems.Add((file.Length / 1024).ToString());
                                lstitm.SubItems.Add(file.Attributes.ToString());
                                lstitm.SubItems.Add(file.FullName.ToString());
                            }
                        }
                        catch
                        { }
                    }
                }
                catch ()
                {                  
                }
            }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
locerst
  • 3
  • 3

2 Answers2

1

Your D: drive contains a folder "System Volume Information" that you don't have the privileges to access. So you will need to either not access it, or catch the exception and handle it to your liking. Not having access to a folder is not uncommon outside of ones own PC, so you might want to think about handling that scenario in your user interface. Maybe paint the folder in grey or display a lock icon or something.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

There was a trick to do it. Do Enable Sharing to this folder.

For more information id here

Or do this trick .....

 static void RecursiveGetFiles(string path)
{
    DirectoryInfo dir = new DirectoryInfo(path);
    try
    {

        foreach (FileInfo file in dir.GetFiles())
        {
            MessageBox.Show(file.FullName);
        }
    }
    catch (UnauthorizedAccessException)
    {

        Console.WriteLine("Access denied to folder: " + path);
    }

    foreach (DirectoryInfo lowerDir in dir.GetDirectories())
    {
        try
        {
            RecursiveGetFiles(lowerDir.FullName);

        }
        catch (UnauthorizedAccessException)
        {

            MessageBox.Show("Access denied to folder: " + path);
        }
    }
}

}

kamil Krasinsky answered it .. here

Community
  • 1
  • 1
matzone
  • 5,703
  • 3
  • 17
  • 20
  • hi i need code for my program that i will can detect what i selected is file or folder i used some code but some of folder have "." in name that this code do not work very good is there any magic alike code for it :) – locerst May 23 '13 at 07:25