如何做到這一點:C#的文件夾和子文件夾
- 檢查上傳文件夾存在(如果也沒有那麼創建它)〜/上傳
- 檢查是否有用戶名子文件夾存在〜/上傳/與用戶名文件夾
它來檢查每個單獨的情況下,因爲我可以添加一個新用戶的系統和過程的文件夾上傳alredy存在,只需要創建一個子文件夾的用戶名
如何做到這一點:C#的文件夾和子文件夾
它來檢查每個單獨的情況下,因爲我可以添加一個新用戶的系統和過程的文件夾上傳alredy存在,只需要創建一個子文件夾的用戶名
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);
簡而言之:
HttpServerUtility.MapPath
到虛擬路徑轉換(如~/uploads
)到一個物理路徑。Directory.Exists
來確定目錄是否存在Directory.CreateDirectory
來創建它。試試這個:
var path = string.Format(@"{0}Uploads\{1}",Request.PhysicalApplicationPath,
HttpContext.Current.User.Identity.Name);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
你可以試試這個:
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);
}
}
我讓你來包裝這件事通過調用根據您具體的邏輯和paramaterizing在循環的方法用戶名和文件夾