[英]Folder Rename using Directory.Move(): Access to Path is Denied
使用目錄重命名文件夾時,我有公共網絡共享文件夾。 Move():訪問路徑被拒絕,實際問題是當另一個系統或另一個進程打開文件夾時,如果有可能重命名文件夾或關閉由另一個系統或另一個進程打開的文件夾
我的代碼
static void moveToShare()
{
if (!Directory.Exists("\\Share")) Directory.CreateDirectory("\\Share");
string timestamp = DateTime.Now.ToString("yyyyMMddHHmms");
try
{
Directory.Move("\\Output", "\\Share\\" + timestamp);
}
catch(Exception e)
{
Console.WriteLine("Cant Move Folder: " + e.Message);
}
}
您必須獲得訪問權限。
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();
if (!Directory.Exists("\\Share"))
Directory.CreateDirectory("\\Share");
string timestamp = DateTime.Now.ToString("yyyyMMddHHmms");
try
{
Directory.Move("\\Output", "\\Share\\" + timestamp);
}
catch(Exception e)
{
Console.WriteLine("Cant Move Folder: " + e.Message);
}
context.Undo();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.