C#创建文件夹代码
- 时间:2015年04月02日 15:41:09 来源:魔法猪系统重装大师官网 人气:12668
///
///创建文件夹
///
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
}
}
}
}





