如何做到这一点:
它是为了单独检查每个案例,因为我可以向系统添加新用户,当然文件夹上传alredy存在,只需要创建一个带有用户名的子文件夹
答案 0 :(得分:2)
简而言之:
HttpServerUtility.MapPath
将虚拟路径(例如~/uploads
)转换为物理路径。Directory.Exists
确定目录是否存在Directory.CreateDirectory
创建目录。答案 1 :(得分:0)
string userName = ""; // Substitute with logic for obtaining username
string folder = ResolveUrl( "~/Uploads" );
folder = System.IO.Path.Combine( folder, userName );
if ( !System.IO.Directory.Exists( folder ) )
System.IO.Directory.CreateDirectory( folder );
答案 2 :(得分:0)
试试这个:
var path = string.Format(@"{0}Uploads\{1}",Request.PhysicalApplicationPath,
HttpContext.Current.User.Identity.Name);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
答案 3 :(得分:0)
你可以试试这个:
using System;
using System.IO;
public void CreateDirectories(string uploadDirPath, string userName)
{
string userDirPath= uploadDirPath + "\\" + userName ;
if (!Directory.Exists(uploadDirPath))
{
Directory.CreateDirectory(uploadDirPath);
if (!Directory.Exists(userDirPath))
Directory.CreateDirectory (userDirPath);
}
}
我通过根据您的特定逻辑调用循环中的方法以及对用户名和文件夹进行定位来留下它来包装它