C#创建文件夹代码
作者:希维• 更新时间:2023-08-28 22:00:42 •阅读 0
/// ///创建文件夹
/// publicclass Util { [DllImport("msvcrt.dll", SetLastError =true, CharSet = CharSet.Unicode, ExactSpelling =true)] privatestaticexternint _mkdir(string path); /// /// 创建目录 /// /// /// 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 } } } }
声明:希维路由器教程网提供的内容,仅供网友学习交流,如有侵权请与我们联系删除,谢谢。ihuangque@qq.com
本文地址:https://www.ctrlcv.com.cn/diannao/169323124210612.html