不知道如何Directory.CreateDirectory()的作品 [英] Not sure how Directory.CreateDirectory() works

查看:121
本文介绍了不知道如何Directory.CreateDirectory()的作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code在我的控制器:

I have this code in my controller:

public ActionResult Upload(ScormUploadViewModel model)
{
  if (ModelState.IsValid)
  {
    if (model.ScormPackageFile != null)
    {
      string zipCurFile = model.ScormPackageFile.FileName;
      string destinationDirectoryName = Path.GetFullPath(zipCurFile);
      //.GetFileNameWithoutExtension(zipCurFile);
      Directory.CreateDirectory(destinationDirectoryName);
    }
   }
 }

我上传过我的观点一个zip文件,然后需要用相同的名称将它解压缩在同一位置的文件夹中的zipfilename

I upload a zip file through my view and then need to unzip it in the same location in a folder with the same name as the zipfilename

该文件:C:\\ TFS preVIEW \\锌\\网络\\项目\\ ScormPackages \\ Windows 8的SkyDrive的培训 - Spanish.zip

the file is: C:\TFSPreview\Zinc\Web\Project\ScormPackages\Windows 8 Training SkyDrive - Spanish.zip

我需要建立在C文件夹:\\ TFS preVIEW \\锌\\网络\\项目\\ ScormPackages \\
名称为:Windows 8的SkyDrive的培训 - 西班牙

I need to create a folder in C:\TFSPreview\Zinc\Web\Project\ScormPackages\ with the name: Windows 8 Training SkyDrive - Spanish

因而有:C:\\ TFS preVIEW \\锌\\网络\\项目\\ ScormPackages \\ Windows 8的SkyDrive的培训 - 西班牙\\

thus have: C:\TFSPreview\Zinc\Web\Project\ScormPackages\Windows 8 Training SkyDrive - Spanish\

并解压缩在这上面文件夹包含在C中的所有文件:\\ TFS preVIEW \\锌\\网络\\项目\\ ScormPackages \\ Windows 8的SkyDrive的培训 - Spanish.zip

and UNZIP in this above folder all the files contained in C:\TFSPreview\Zinc\Web\Project\ScormPackages\Windows 8 Training SkyDrive - Spanish.zip

所以我的问题是:CreateDirectory()创建的Windows文件夹8培训的SkyDrive - 西班牙在C:\\ TFS preVIEW \\锌\\网络\\项目\\ ScormPackages \\还是会尝试在短短C上创建文件夹:??

so my question is: will CreateDirectory() create the folder Windows 8 Training SkyDrive - Spanish in C:\TFSPreview\Zinc\Web\Project\ScormPackages\ or will it try and create the folder in just c:??

感谢

推荐答案

这将创建在目录C:\\ TFS preVIEW \\锌\\网络\\项目\\ ScormPackages \\ 事实上,它会创建的所有的目录在这条道路,如果他们不存在:

It will create the directory inside C:\TFSPreview\Zinc\Web\Project\ScormPackages\. In fact, it will create all directories in that path if they don't already exist:

任何和路径指定的所有目录中创建的,除非他们
  已经存在或除非路径的某些部分是无效的。路径
  参数指定的目录路径,而不是一个文件路径。如果
  目录已经存在,这种​​方法不会创建一个新的目录,
  但它返回现有目录的DirectoryInfo对象。

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. The path parameter specifies a directory path, not a file path. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.

不过,这code有一个错误: destinationDirectoryName 不在一个目录路径,它是一个的文件的路径内目标目录。所以,你应该做的是

However, this code has a bug: destinationDirectoryName is not the path to a directory, it's a path to a file inside the destination directory. So what you should be doing is

// zipCurFile = C:\...\ScormPackages\Windows 8 Training SkyDrive - Spanish.zip
// Path.GetDirectoryName gives "C:\...\ScormPackages"
// Path.GetFileName gives "Windows 8 Training SkyDrive - Spanish"
// Path.Combine on these two gives you the correct target

Directory.CreateDirectory(
    Path.Combine(
        Path.GetDirectoryName(zipCurFile), Path.GetFileName(zipCurFile));

这篇关于不知道如何Directory.CreateDirectory()的作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆