西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)C#.NET → C#創(chuàng)建文件夾代碼

C#創(chuàng)建文件夾代碼

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2010/11/23 9:32:58字體大。A-A+

作者:佚名點擊:1130次評論:0次標簽: 文件夾

  • 類型:文件處理大小:1.6M語言:中文 評分:5.0
  • 標簽:
立即下載

///<summary>
   
///創(chuàng)建文件夾

     ///</summary>
    publicclass Util
    {
        [DllImport(
"msvcrt.dll", SetLastError =true, CharSet = CharSet.Unicode, ExactSpelling =true)]
       
privatestaticexternint _mkdir(string path);

       
///<summary>
       
/// 創(chuàng)建目錄
       
///</summary>
       
///<param name="path"></param>
       
///<returns></returns>
        publicstatic DirectoryInfo CreateDirectory(string path)
        {
            DirectoryInfo oDir
=new DirectoryInfo(Path.GetFullPath(path));

           
try
            {
               
if (!oDir.Exists)
                {
                    oDir.Create();
                }
               
return oDir;
            }
           
catch
            {
                CreateDirectoryUsingDll(oDir);
               
returnnew DirectoryInfo(path);
            }
        }

       
privatestaticvoid CreateDirectoryUsingDll(DirectoryInfo dir)
        {
            ArrayList oDirsToCreate
=new ArrayList();

           
while (dir !=null&&!dir.Exists)
            {
                oDirsToCreate.Add(dir.FullName);
                dir
= dir.Parent;
            }

           
if (dir ==null)
            {
               
throw (new System.IO.DirectoryNotFoundException("Directory \"" + oDirsToCreate[oDirsToCreate.Count - 1] + "\" not found."));
            }

           
for (int i = oDirsToCreate.Count -1; i >=0; i--)
            {
               
string sPath = (string)oDirsToCreate[i];
               
int iReturn = _mkdir(sPath);

               
if (iReturn !=0)
                {
#if DEBUG
                   
thrownew ApplicationException("Error calling [msvcrt.dll]:_wmkdir("+ sPath +"), error code: "+ iReturn);
#else
                   
thrownew ApplicationException();
#endif
                }
            }
        }
    }

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)