0
votes

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.

I am working on a .net application which is used to compare code changes between builds.

part of the application that I am attempting to add utilizes beyond compare to generate an html comparison of two chunks of code.

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"

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
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
_Default.gridView_SelectedIndexChanged(Object _sender, EventArgs e) +1024 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1241
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean 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
  • Add Everyone with full permissions to the wwwroot folder
  • 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.

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!

2
Do you need to store the file under C:\inetpub\wwwroot? Can you try putting it in a subfolder?Paritosh
Sorry, I should have included this in my original post. I already tried putting it in a subfolder as well as a folder completely outside of the structure. Same results in both cases.Christopher Gerdes

2 Answers

0
votes

produce your error...

Created an web application and write to file in the host directory "C:\inetpub\wwwroot\" by using following code

 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. I got the following error..,

Source Error: 

Line 14: { Line 15: string s = "abcdefghijklmnopqrstuvwxyz"; Line 16: System.IO.File.WriteAllText(WorkingFolder + @"\File1.txt", s); Line 17: } Line 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....

Double Check the Folder access permission. Definitely can write.. I am using IIS7.5

0
votes

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.