Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在根驱动器(如D)中搜索时出错:\_C#_Directory - Fatal编程技术网

C# 在根驱动器(如D)中搜索时出错:\

C# 在根驱动器(如D)中搜索时出错:\,c#,directory,C#,Directory,当我选择任何文件夹(使用search option=SearchOption.AllDirectories)时,我编写此代码并工作,但是 像D:\一样驾驶时出错 “对路径D:\系统卷信息的访问被拒绝” 我将“\”添加到此路径,但仍然得到错误 if (dirListBox.Items.Count == 0) { foreach (int Index in disksListBox.CheckedIndices) {

当我选择任何文件夹(使用
search option=SearchOption.AllDirectories
)时,我编写此代码并工作,但是

D:\
一样驾驶时出错

“对路径D:\系统卷信息的访问被拒绝”

我将
“\”
添加到此路径,但仍然得到错误

        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 ()
                {                  
                }
            }
if(dirListBox.Items.Count==0)
{
foreach(disksListBox.CheckedIndices中的int索引)
{
String Dir=disksListBox.Items[Index].ToString().Substring(0,2);
Dir+=@“\”;
如果(检查存在(目录))
{
直接添加(直接);
}
}
}
其他的
{
对于(int Index=0;Index
您的D:驱动器包含一个文件夹“系统卷信息”您没有访问权限。因此,您需要要么不访问它,要么捕获异常并根据自己的喜好进行处理。在个人电脑之外,无法访问文件夹并不少见,因此您可能需要考虑在用户界面中处理该情况。可能会将文件夹涂成灰色或显示锁定图标或有些东西。

有个窍门。请启用共享到此文件夹

有关详细信息,请访问id

或者做这个把戏

 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回答了这一问题。

我们无权访问每个驱动器中的文件夹系统卷信息。。但是Bill Gatesy您的尝试。.catch没有阻止错误?不,它不会让所有驱动器都被释放。反正是要搜索D:\?嗨,我需要我的程序的代码,我可以检测到我选择的是文件或文件夹。我使用了一些代码,但是有些文件夹名称中有“.”说明此代码不太好用。是否有类似于魔术的代码:)