///<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
}
}
}
}