在我的ASP.NET WEbForms
应用中,我想创建一个文件夹并在其中保存文件。在我的项目中,我有一个名为CRMImages/Projects
的文件夹。我想在Projects文件夹&中创建一个子文件夹。保存他们的图像。目前,我从CRMImages /检索图像作为父文件夹。
这是我在代码隐藏方面的代码:
try
{
string pathToCreate = "~/CRMImages/Projects/" + item.ProjectId;
string myFileName = "";
if (!Directory.Exists(Server.MapPath(pathToCreate)))
{
DirectoryInfo di = Directory.CreateDirectory(pathToCreate);
var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var userName = user.Translate(typeof(System.Security.Principal.NTAccount));
System.Security.AccessControl.DirectorySecurity sec = di.GetAccessControl();
sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(userName,
System.Security.AccessControl.FileSystemRights.Modify,
System.Security.AccessControl.AccessControlType.Allow));
di.SetAccessControl(sec);
Directory.CreateDirectory(pathToCreate);
System.Diagnostics.Debug.WriteLine("FOLDER CREATED PATH : " + di.FullName);
myFileName = pathToCreate + "/projectLogo.png";
System.Diagnostics.Debug.WriteLine("PATH To Save Logo File & NAME : " + myFileName);
/*
if (File.Exists(item.ProjectLogoUrl) ) {
FileUpload projLogoUpload = new FileUpload();
if (projLogoUpload.HasFile) {
myFileName = pathToCreate + "/projectLogo.png";
projLogoUpload.SaveAs(myFileName);
}
// panFileBtn.SaveAs(filePath);
} */
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("EXCEPTION While SAving File : " + ex.Message + "\n *** STACK" + ex.StackTrace);
}
代码执行,但我没有在项目文件夹中看到创建的文件夹。让我们说item.ProjectId的值是“EMP3”,我在上面的代码执行中看到的日志是:
FOLDER CREATED PATH : C:\Program Files (x86)\IIS Express\~\CRMImages\Projects\EMP3
PATH To Save Logo File & NAME : ~/CRMImages/Projects/EMP3/projectLogo.png
我查看了IIS Express
文件夹&在那里创建了这个完整的路径。你能说出为什么在IISExpress&如何在我的项目中已存在的/CRMImages/Projects
文件夹中创建文件夹!!
非常感谢任何帮助。 感谢
答案 0 :(得分:1)
你必须替换下面的行
DirectoryInfo di = Directory.CreateDirectory(pathToCreate);
使用
DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(pathToCreate));