大家好,我正在尝试使用C# MVC3创建文件夹。我在我的控制器中有以下代码:
string path = Path.Combine("~/Content/albums", album.title.Replace(" ", ""));
Directory.CreateDirectory(path);
但是它似乎没有创建文件夹。我已经尝试过使用不带相对路径的directory,它可以正常工作。
Directory.CreateDirectory("c:/test");
谢谢你
大家好,我正在尝试使用C# MVC3创建文件夹。我在我的控制器中有以下代码:
string path = Path.Combine("~/Content/albums", album.title.Replace(" ", ""));
Directory.CreateDirectory(path);
但是它似乎没有创建文件夹。我已经尝试过使用不带相对路径的directory,它可以正常工作。
Directory.CreateDirectory("c:/test");
谢谢你
如果在控制器中,请首先尝试使用Server.MapPath;如果在控制器外部,则尝试使用System.Web.Hosting.HostingEnvironment.MapPath。它们都能够将虚拟资源路径映射到物理路径。因此:
string contentPath = Server.MapPath("~/Content/albums");
string path = Path.Combine(contentPath, album.title.Replace(" ", ""));
然后创建目录。
尝试使用这个替代方案,因为你指出的路径无效。
string physicalPath = Server.MapPath("~/Content/albums");
string path = Path.Combine(physicalPath , album.title.Replace(" ", ""));
Directory.CreateDirectory(path);