西西软件园多重安全检测下载网站、值得信赖的软件下载站!
软件
软件
文章
搜索

首页编程开发C#.NET → C#创建文件夹代码

C#创建文件夹代码

相关软件相关文章发表评论 来源:本站整理时间:2010/11/23 9:32:58字体大小:A-A+

作者:佚名点击:1130次评论:0次标签: 文件夹

  • 类型:文件处理大小:1.6M语言:中文 评分:5.0
  • 标签:
立即下载

///<summary>
   
///创建文件夹

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

       
///<summary>
       
/// 创建目录
       
///</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
                }
            }
        }
    }

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)