相關代碼如下:
 DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
                if (dirInfo.GetFiles().Length != 0)
                {
                 //If Block
                }
在執行GetFiles方法時拋出DircetoryNotFoundException,但是我檢查了無數次,確認dirPath這個路徑是存在的。

解决方案 »

  1.   

    是asp.net吗?没有权限吧:参见
    http://www.cnblogs.com/qiao198/archive/2005/11/12/274732.html文章中的用户可能不对
    实际用户是:
    NETWORK SERVICE -- WIN2003
    ASPNET -- winxp
      

  2.   

    找不到dirPath描述的目录名。可能是你在构造dirPath时里面的字符错了。
      

  3.   

    好吧,全部代碼都貼出來,這段代碼就是刪除IE或者Opera瀏覽器的緩存,是在windows Mobile下的。在Debug前我確認IE或者Opera已經被Kill掉了。而且我只在傳入Opera的時候拋出這個異常,在傳入IE的時候卻不傳入。
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;namespace ClearBrowserCache
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamWriter stw = new StreamWriter(args[1], false);
                bool flag = false;
                string browserCacheFolder;
                switch (args[0].ToUpper())
                {
                    case "IE":
                        browserCacheFolder = @"\Windows\Profiles\guest\Temporary Internet Files\Content.IE5\";
                        flag = ClearDirectory(browserCacheFolder);
                        break;
                    case "OPERA":
                        browserCacheFolder = @"\Application Data\Oprea9\cache\";
                        flag = ClearDirectory(browserCacheFolder);
                        break;
                    default:
                        break;
                }
                stw.Write(flag.ToString());
                stw.Close();
            }
            static bool ClearDirectory(string dirPath)
            {
                //try
                //{
                    DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
                    if (dirInfo.GetFiles().Length != 0)
                    {
                        foreach (FileInfo f in dirInfo.GetFiles())
                            f.Delete();
                    }
                    if (dirInfo.GetDirectories().Length != 0)
                    {
                        foreach (DirectoryInfo d in dirInfo.GetDirectories())
                            d.Delete(true);
                    }
                    if (Directory.GetDirectories(dirPath).Length == 0 && Directory.GetFiles(dirPath).Length == 0)
                        return true;
                    else
                        return false;
                //}
                //catch
                //{
                //    return false;
                //}
            }
        }
    }
      

  4.   

    通过exist判断目录是否存在和调试看看dirPath值
      

  5.   

    using System;
    using System.IO;public class DirectoryInfoTest
    {
    public static void Main()
    {
    string m_path = @"c:\files";                                  //设置所要检测文件夹的绝对路径
    DirectoryInfo m_DirInfo = new DirectoryInfo(m_path);           //以路径为参数构造DirectoryInfo对象
    if (m_DirInfo.Exists)                                         //检测文件夹是否存在
    {
    Console.WriteLine("Directory Exists!");
    }
    else
    {
    Console.WriteLine("Directory Not Exists!");
    }
    Console.ReadLine();
    }
    }