简体   繁体   English

使用 Directory.Move() 重命名文件夹:拒绝访问路径

[英]Folder Rename using Directory.Move(): Access to Path is Denied

I have Common Network Shared Folder When Folder Rename using Directory.使用目录重命名文件夹时,我有公共网络共享文件夹。 Move(): Access to Path is Denied, Actual Problem is When Folder is open by another System or another process, If Any possible to rename the folder or Close the Folder Open by another system or another process Move():访问路径被拒绝,实际问题是当另一个系统或另一个进程打开文件夹时,如果有可能重命名文件夹或关闭由另一个系统或另一个进程打开的文件夹

My Code我的代码

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);
    }
}

You must gain access.您必须获得访问权限。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM