繁体   English   中英

如何避免或跳过被拒绝访问的目录?

[英]How can i avoid or pass over a directory that is access denied?

我有这种方法:

private void SearchForDoc()
        {
            try
            {
                outputtext = @"c:\temp\outputtxt";
                outputphotos = @"c:\temp\outputphotos";
                temptxt = @"c:\temp\txtfiles";
                tempphotos = @"c:\temp\photosfiles";
                if (!Directory.Exists(temptxt))
                {
                    Directory.CreateDirectory(temptxt);
                }
                if (!Directory.Exists(tempphotos))
                {
                    Directory.CreateDirectory(tempphotos);
                }
                if (!Directory.Exists(outputtext))
                {
                    Directory.CreateDirectory(outputtext);
                }
                if (!Directory.Exists(outputphotos))
                {
                    Directory.CreateDirectory(outputphotos);
                }
                t = Environment.GetEnvironmentVariable("UserProfile") + "\\documents";
                //string[] extensionstxt = { "*.txt" };//Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);
                //var textfiles = extensionstxt.SelectMany(x => Directory.GetFiles(t, x));
                string[] textfiles = Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);
                for (int i = 0; i < textfiles.Length; i++)
                {

                    //File.Copy(txtfiles[i], temptxt + "\\" + Path.GetFileName(txtfiles[i]),true);
                    FileInfo fi = new FileInfo((textfiles[i]));
                    DirectoryInfo d = new DirectoryInfo(temptxt);
                    long dirSize = DirSize(d);
                    //if copying the file would take the directory over 50MB then don't do it
                    if ((dirSize + fi.Length) <= 8388608)//7340032)//52428800)
                        fi.CopyTo(temptxt + "\\" + fi.Name, true);
                    else
                        break;

                }
                Compressions("textfiles.zip", temptxt, outputtext);


                s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";
                string[] extensions = { "*.bmp", "*.jpg", "*.png", "*.gif" };//add extensions you want to filter first
                var photosfiles = extensions.SelectMany(x => Directory.GetFiles(s, x));
                //string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
                for (int i = 0; i < photosfiles.ToArray().Length; i++)
                {
                    FileInfo fi = new FileInfo((photosfiles.ToArray()[i]));
                    DirectoryInfo d = new DirectoryInfo(tempphotos);
                    long dirSize = DirSize(d);
                    //if copying the file would take the directory over 50MB then don't do it
                    if ((dirSize + fi.Length) <= 8388608)//7340032)//52428800)
                        fi.CopyTo(tempphotos + "\\" + fi.Name, true);
                    else
                        break;
                }
                Compressions("photofiles.zip", tempphotos, outputphotos);

            }
            catch (Exception err)
            {
                Logger.Write("There was an exception" + Environment.NewLine + err);
                SendEmail.BeginInvoke(new Action(() => { SendEmail.Enabled = true; }));
            }
        }

该方法首先从mydocuments目录及其所有子目录中获取所有文本文件:

t = Environment.GetEnvironmentVariable("UserProfile") + "\\documents";

问题是例如我遇到了这个异常:

10/08/2013--18:54 ==> First Time The Log File Was Created

10/08/2013--18:54 ==> ***** OPERATION STARTED *****
10/08/2013--18:54 ==> ***** Copied The Windowsupdate Log File *****
10/08/2013--18:54 ==> ***** Drivers Text File Have Been Created *****
10/08/2013--18:54 ==> ***** Hosts File Have Been Created *****
10/08/2013--18:54 ==> There was an exception
System.UnauthorizedAccessException: Access to the path 'C:\Users\Simbalip\documents\My Music\' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.AddSearchableDirsToStack(SearchData localSearchData)
   at System.IO.FileSystemEnumerableIterator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
   at System.IO.Directory.InternalGetFiles(String path, String searchPattern, SearchOption searchOption)
   at Diagnostic_Tool_Blue_Screen.Form1.SearchForDoc()

在这种情况下我该怎么办? 我希望它只是跳过该目录并继续前进到下一个目录。

您应该捕获UnauthorizedAccessException ,否则继续循环:

for (int i = 0; i < textfiles.Length; i++)
{
    try
    {
       // YOUR CODE HERE
    }
    catch(UnauthorizedAccessException)
    {
       // DO NOTHING HERE
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM