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:ProgramControllersMyController.cs:line 201 InnerException:
我已经检查了我正在模拟的帐户的权限,并且该帐户对我要写入的文件夹具有修改,读取和执行,列出文件夹内容,读取和写入权限。
也许我错过了关于文件权限的东西? 错误是当我试图将文件移动到 server123 D $ Archive上的存档时。 我知道,当我在C: Temp Archive(我的机器)上存档文件时,程序完美地工作。 我也可以写一个新的文件没有任何麻烦 server123 testing。 错误是因为我正在从服务器的一个驱动器移动到另一个? 如果是这样的话…有没有办法绕过它,所以我可以从 server123 Test写入 server123 D $ Archive?
SEEK_HOLE总是指向文件的结尾
[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(@"\server123Test","cartons.xml"); string archiveFilePath = Path.Combine(@"\server123D$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(@"\server123Test","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); }
请帮忙!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。