c# - 移动文件时未授权访问,但我模拟的帐户有权限?

标签 c# windows file permissions

在我的 MVC 网站上,有一个页面归档一个旧文件并写入一个新文件。

但是,当我归档旧文件时,出现以下错误:

System.UnauthorizedAccessException was caught
  HResult=-2147024891
  Message=Access to the path is denied.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.__Error.WinIOError()
       at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost)
       at System.IO.File.Move(String sourceFileName, String destFileName)
       at Controller.Action in C:\Program\Controllers\MyController.cs:line 201
  InnerException: 

我已经检查了我正在模拟的帐户的权限,该帐户对我要写入的文件夹具有修改、读取和执行、列出文件夹内容、读取和写入权限。

也许我遗漏了一些关于文件权限的信息?错误是当我试图将文件移动到\server123\D$\Archive 上的存档时。我知道当我将文件存档在 C:\Temp\Archive(我的机器)时,该程序可以完美运行。我还可以毫不费力地向\server123\Test 写入一个新文件。错误可能是因为我正在从服务器的一个驱动器移动到另一个驱动器吗?如果是这种情况...是否有解决方法,以便我可以从\server123\Test 写入\server123\D$\Archive?

这是我用来移动文件的代码。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MoveFile(MoveFileViewModel model)
{
    string cartonsXml = GetCartonsXml(model);

    try
    {
        //Impersonate user who has access to both folders.
        string user = ConfigurationManager.AppSettings["FileUser"].ToString();
        string domain = ConfigurationManager.AppSettings["FileDomain"].ToString();
        string password = ConfigurationManager.AppSettings["FilePassword"].ToString();

        ImpersonateUser impersonateUser = new ImpersonateUser();
        IntPtr token = impersonateUser.GetUserToken(user, domain, password);

        if (token != IntPtr.Zero)
        {
            using (WindowsImpersonationContext wic = WindowsIdentity.Impersonate(token))
            {
                //Move old cartons.xml file to archive.
                string oldCartonsFilePath = Path.Combine(@"\\server123\Test", "cartons.xml");
                string archiveFilePath = Path.Combine(@"\\server123\D$\Archive", "cartons("
                    + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ")." + Guid.NewGuid().ToString() + ".xml");

                System.IO.File.Move(oldCartonsFilePath, archiveFilePath); //This is where I catch the exception!

                //Write new cartons.xml file.
                string newCartonsFilePath = Path.Combine(@"\\server123\Test", "cartons.xml");

                using (StreamWriter sw = new StreamWriter(newCartonsFilePath))
                {
                    sw.WriteLine(cartonsXml);
                    sw.Close();
                }

                ViewBag.MsgText = "Complete!";
                ViewBag.MsgColor = "Green";
            }
        }
        else
        {
            ViewBag.MsgText = "Credentials failed! Files not moved!";
            ViewBag.MsgColor = "Red";
        }
    }
    catch (Exception ex)
    {
        ViewBag.MsgText = ex.Message;
        ViewBag.MsgColor = "Red";
    }

    return View(model);
}

求助! :(

最佳答案

经过各种试验和错误后,我创建了我尝试访问的文件夹的共享,授予了我正在使用的帐户的读/写权限,现在代码可以正常工作了。我将根据我在这里看到的评论更新我的代码。谢谢大家!

关于c# - 移动文件时未授权访问,但我模拟的帐户有权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23769982/

相关文章:

c# - 组合多个元素集合的优雅方式?

c# - Windows 和/或用户控件之间通信的模式/方法

c++ - 与遗留 TCHAR 代码接口(interface)的正确样式

macos - 批量删除文件扩展名 OSX

c# - 在 WebAPI 中使用自定义 IHttpActionInvoker 进行异常处理

c# - 忽略 AutoMapper 中的属性?

windows - MD 和 MKDIR 批处理命令有什么区别?

更改默认 mkstemp 的文件权限

python - python 脚本临时文件中的 Vim 编辑器

c# - 通过代码将文本框的高度绑定(bind)到父级