简体   繁体   English

尝试从asp.net应用程序写入文件时出现System.UnauthorizedAccessException

[英]System.UnauthorizedAccessException when attempting to write a file from an asp.net application

Edit: I have found the solution. 编辑:我找到了解决方案。 I had thought the error was being generated when attempting to write the files to be compared, in reality it was being generated when I attempted to read the file to be compared. 我以为错误是在尝试写入要比较的文件时生成的,实际上是在我尝试读取要比较的文件时生成的。 I had a syntax error in the command being called for the comparison executable and that was resulting in the results file not being generated. 我在比较比较可执行文件的调用命令中出现语法错误,这导致未生成结果文件。 I was seeing the access violation when I tried to read a non-existent file (yes, I should have been checking for the file existence). 我在尝试读取不存在的文件时遇到访问冲突(是的,我应该一直在检查文件是否存在)。 I'm having issues getting BC to generate the comparison (for some reason the exact same command works from the command prompt but does not work when ran from C#), but I'll ask that as a separate question if necessary. 我在让BC生成比较时遇到问题(由于某些原因,完全相同的命令可从命令提示符下运行,但从C#运行时不起作用),但如有必要,我将作为一个单独的问题提出。

I am working on a .net application which is used to compare code changes between builds. 我正在.net应用程序上工作,该应用程序用于比较内部版本之间的代码更改。

part of the application that I am attempting to add utilizes beyond compare to generate an html comparison of two chunks of code. 我尝试添加的应用程序的一部分利用了无法比较的方式来生成两个代码块的html比较。

In order to do this I am generating files from the code (note that I know the file names will have a conflict issue for multiple users, I will fix this later), then calling the beyond compare executable from command line. 为了做到这一点,我要从代码中生成文件(请注意,我知道文件名将对多个用户产生冲突问题,我将在以后进行修复),然后从命令行调用超越比较可执行文件。 I generate the files using the below code: 我使用以下代码生成文件:

//write the code to a local disk file
            System.IO.File.WriteAllText(@workingFolder + "\\File1.txt", (String)dataRow["OldMethodCode"]);
            System.IO.File.WriteAllText(@workingFolder + "\\File2.txt", (String)dataRow["NewMethodCode"]);

The value for workingFolder is this: "C:\\inetpub\\wwwroot" workingFolder的值是这样的:“ C:\\ inetpub \\ wwwroot”

The problem that I am running into is that when this code is ran and it attempts to create those files I get the below exception: 我遇到的问题是,运行此代码并尝试创建这些文件时,出现以下异常:

[UnauthorizedAccessException: Access to the path 'C:\\inetpub\\wwwroot' is denied.] System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +214 [UnauthorizedAccessException:对路径'C:\\ inetpub \\ wwwroot'的访问被拒绝。] System.IO .__ Error.WinIOError(Int32 errorCode,字符串可能是FullPath)+216 System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)+1430 System.IO.FileStream..ctor(字符串路径,文件模式模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)+214
System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) +187 System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost) +90 System.IO.StreamReader..ctor(字符串路径,编码编码,布尔型detectEncodingFromByteOrderMarks,Int32 bufferSize,布尔值检查主机)+187 System.IO.File.InternalReadAllText(字符串路径,编码方式,布尔值检查主机)+90
_Default.gridView_SelectedIndexChanged(Object _sender, EventArgs e) +1024 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1241 _Default.gridView_SelectedIndexChanged(Object _sender,EventArgs e)+1024 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e,Boolean causeValidation,String validateGroup)+1241
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+3804

I have tried a number of things to resolve this. 我已经尝试了许多方法来解决此问题。 As with any problem, you can lose track of all your options but here are the ones that I thought had the highest chance of success (but didn't work): 像任何问题一样,您可能会忘记所有选择,但以下是我认为成功可能性最高(但没有用)的那些选择:

  • Add networkService as full permissions to the wwwroot folder 将networkService作为完全权限添加到wwwroot文件夹
  • Add Everyone with full permissions to the wwwroot folder 将具有完全权限的所有人添加到wwwroot文件夹
  • Change the application pool managed pipeline mode to classic 将应用程序池托管管道模式更改为经典
  • Set the app pool to run with a custom user and give that user full permissions to the folder 将应用程序池设置为与自定义用户一起运行,并授予该用户对该文件夹的完全权限
  • Give just about any other user that seems to be related full privileges to the folder 授予几乎所有与该文件夹相关的所有特权的其他用户
  • Move the folder to another local directory outside of wwwroot and point to that, and try all of the above with that folder. 将文件夹移动到wwwroot之外的另一个本地目录并指向该目录,然后使用该文件夹尝试上述所有操作。

In all cases, I can't seem to create the file, much less access it to run the comparison. 在所有情况下,我似乎都无法创建文件,更不用说访问它来运行比较了。

Does anyone have advice to offer on how to resolve this issue, or an alternative/better way to do the comparison that would not require file creations? 有没有人提供关于如何解决此问题的建议,或进行不需要创建文件的比较的另一种/更好的方式?

Thanks! 谢谢!

produce your error... 产生你的错误...

Created an web application and write to file in the host directory "C:\\inetpub\\wwwroot\\" by using following code 使用以下代码创建了一个Web应用程序并写入主机目录“ C:\\ inetpub \\ wwwroot \\”中的文件

 string WorkingFolder = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = "abcdefghijklmnopqrstuvwxyz";
        System.IO.File.WriteAllText(WorkingFolder + @"\File1.txt", s);
    }

I publish and deploy application under Default App pool.My Default App pool uses Domain User\\IIS_IUSRS account.By default I have only Read & Execute Access to "C:\\inetpub\\wwwroot\\" folder. 我在默认应用程序池下发布和部署应用程序。我的默认应用程序池使用域用户\\ IIS_IUSRS帐户。默认情况下,我仅对“ C:\\ inetpub \\ wwwroot \\”文件夹具有读取和执行访问权限。 I got the following error.., 我收到以下错误。

Source Error: 

Line 14: { Line 15: string s = "abcdefghijklmnopqrstuvwxyz"; 第14行:{第15行:字符串s =“ abcdefghijklmnopqrstuvwxyz”; Line 16: System.IO.File.WriteAllText(WorkingFolder + @"\\File1.txt", s); 第16行:System.IO.File.WriteAllText(WorkingFolder + @“ \\ File1.txt”,s); Line 17: } Line 18: } 第17行:}第18行:}

Source File: D:\Working Samples\IISCheck\IISCheck\Default.aspx.cs    Line: 16 

Stack Trace: 堆栈跟踪:

[UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\File1.txt' is    denied.]
  System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +10555843
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights,  Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options,  SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +2580
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +138
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) +98
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) +139
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding) +17 
System.IO.File.WriteAllText(String path, String contents, Encoding encoding) +60IISCheck._Default.Page_Load(Object sender, EventArgs e) in D:\Working Samples\IISCheck\IISCheck\Default.aspx.cs:16
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
System.Web.UI.Control.OnLoad(EventArgs e) +132
System.Web.UI.Control.LoadRecursive() +66
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

Now I give Full Control permission to IIS_IUSRS for "C:\\Inetpub\\wwwroot" Directory.... It works and can able to write to the file.... 现在,我将“ C:\\ Inetpub \\ wwwroot”目录的IIS_IUSRS授予“完全控制”权限。...它可以工作并且可以写入文件。

Double Check the Folder access permission. 仔细检查文件夹访问权限。 Definitely can write.. I am using IIS7.5 绝对可以写..我正在使用IIS7.5

Ok, this was actually caused later on in the code. 好的,这实际上是稍后在代码中引起的。 The error was triggering when I attempted to read the report in, which hadn't actually been generated. 当我尝试读入报告时(实际上未生成),该错误正在触发。

I'm having issues getting BC to generate the report when ran from C# (The exact same command works from the command prompt) but I will ask a separate question on this if necessary. 从C#运行时,让BC生成报告时遇到了问题(完全相同的命令在命令提示符下起作用),但如有必要,我将对此提出另一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试保存文件时,Sharepoint中托管的ASP.NET ascx控件获取System.UnauthorizedAccessException - ASP.NET ascx control hosted in Sharepoint gets System.UnauthorizedAccessException when trying to save file 尝试从LocalFolder打开文件时出现“ System.UnauthorizedAccessException” - 'System.UnauthorizedAccessException' when trying to open file from LocalFolder 从异步任务创建文件时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when creating a file from Async task 通过链接启动应用程序时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when launch application by link 上载文件时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when uploading a file 将文件解压缩到Desktop时出现“System.UnauthorizedAccessException” - “System.UnauthorizedAccessException” when extracting file to Desktop Xamarin中的ASP.NET MVC 3项目给出了System.UnauthorizedAccessException错误 - ASP.NET MVC 3 project in Xamarin gives System.UnauthorizedAccessException error ASP.NET Core System.UnauthorizedAccessException:访问路径“C:\\...”被拒绝 - ASP.NET Core System.UnauthorizedAccessException: Access to the path 'C:\..." is denied 从android(Xamarin)System写入网络文件夹中。UnauthorizedAccessException - Write on a network folder from android (Xamarin) System.UnauthorizedAccessException 加载XML文件时出现System.UnauthorizedAccessException错误 - Getting System.UnauthorizedAccessException error when loading the XML file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM