Access denied to web server file - c#

I'm working on a web app that runs Excel reports and I'm using an Excel template file that's saved on the server. It works fine when I run it locally but when I try to run the Excel report on the server I get an error saying access is denied to the template file that's saved in the same directory, but I can download the template file when I navigate to it in the browser.
I granted everyone full access to the file to see if that was the issue but I still get the error. What am I overlooking?
error errorId="05d6462e-8bb5-401d-85bc-cd4d9e36fc90" application="/LM/W3SVC/8/ROOT" host="USFLWCT0" type="System.UnauthorizedAccessException" message="Access to the path '\usflwct0\D$\SherlockRoot\Files\Volume_by_Week_Template.xlsx' is denied." source="mscorlib" detail="System.UnauthorizedAccessException: Access to the path '\usflwct0\D$\SherlockRoot\Files\Volume_by_Week_Template.xlsx' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at
edit:
Here's the code used to open the file. The path is hardcoded for now
using (var template = System.IO.File.OpenRead(#"\\usflwct0\D$\SherlockRoot\Files\Volume_by_Week_Template.xlsx"))

You are using one of the admin shares that Windows creates for each drive, in this case the D$ share. They are only accessible by admin level users. You should share the folder you want directly, for example, share the D:\SherlockRoot\Files folder as SherlockFiles that was you can access the content like this \\usflwct0\SherlockFiles\Volume_by_Week_Template.xlsx or in code:
var file = #"\\usflwct0\SherlockFiles\Volume_by_Week_Template.xlsx"
using (var template = System.IO.File.OpenRead(file))

If you want the application to read the the template file, put it in the App_Data folder

Related

Access to Path Denied - Magick.NET.net40.7.4.3.0

I am trying to read text from images using IronOCR. It worked fine on my dev machine but when I try run it through IIS on another machine I am getting the following error
Access to the path 'C:\Windows\TEMP\Magick.NET.net40.7.4.3.0\Magick.NET-Q8-x64.Native.dll' is denied.
there is no Magick.NET.net40.7.4.3.0 folder in the window temp folder location on the dev machine, but it was there on the other machine but it was empty. I deleted the folder and now it is saying there is access denied on the user app_data folder.
I am totally confused as to why it is looking for access to Magick.NET.net40.7.4.3.0, is there some connection between the 2?
In your code you'll need to set :
IronOcrInstallation.InstallationPath = "d:\newpath"
where d:\newpath is a directory on your IIS that your worker process or IIS has access to.
BTW IronOCR is a commercial re-bundling of the open source Magick.NET image and Tesseract OCR libraries.

Getting IIS Server directory path

I am trying to get the IIS server directory path from another computer to download few files from there.
using codes like this..
using (var client = new WebClient())
{
client.DownloadFile("//serverpath", "a.exe");
}
I would like to download the files from the directory shown in the picture!
That is the server computer and I'll be putting some exe files in that directory .I would now like to download the files that i put in that directory to the client computer I'm am using. How should I put the serverpath?? Agent folder is an virtual directory.
If this is asp.net mvc there is direct support for this with FileResult, and Controller has a set of helpers:
In your action:
return File(filename, contentType);

ASP.NET MVC Deployed Application Error 'Could not find a part of the path C:\Users\UserName\Documents\FileName.xlsx' Only in IE

Hi I Have a MVC application that uploads a file to the server, all works fine on my local machine, as soon as I deploy to the server, it only works in Chrome and not in IE. The application reads a file and saves it on the server, I have added given. I have given the correct permissions to the Network Service to access the folder. Although when I run the application outside of the server in IE it fails with the above error. Please assist, any ideas? Please see code below.
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
List<CleanSupplierClaim> supplierClaimsData = new List<CleanSupplierClaim>();
try
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var targetPath = Server.MapPath("~/upload");
file.SaveAs(Path.Combine(targetPath, fileName));
Here is my app pool config, set as the local system
And here is the folder permission setting.
I have allocated the correct permissions for the user reading file from users machine, which in my case is the Network Service. Although in IE it throws an exception 'Could not find a part of the path ''
How can I fix this issue, I have ran out of ideas. My IIS version is 8.5
What if Writing: var targetPath = Server.MapPath("/upload");
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

Windows service accessing invalid directory with StreamReader

I am reading a file that is located in the same directory as my executable using a StreamReader with the following method:
StreamReader reader=new StreamReader(".\\file.txt"); //NOTE: 2nd backslash is escape character in C#
When I do this in the debug environment, it reads the file fine, but when I install the service it tries to read the file in C:\Windows\System32\ as if the working directory is set to that path, but in the services properties there is no working directory option. I'm guessing it's using the working dir of sc.exe.
Is there a way I can get it to resolve to the location of the current executable using relative file paths? Because the service might be placed in different locations based on deployments.
Yes, working directory of a service is %WinDir%\System32.Also GetModuleFileName() will return incorrect result because your service is hosted by another executable (by chance placed in that directory too).
You have to find executing assembly and its location, longer to describe than to do:
string assemblyPath = Assembly.GetExecutingAssembly().Location;
Now just extract directory name and combine with file you want:
string path = Path.Combine(Path.GetDirectoryName(assemblyPath), "file.txt");

UnauthorizedAccessException when downloading a file using the TFS SDK

When I try to download a file from TFS version control SDK to my computer I receive an 'UnauthorizedAccessException' saying Access to the local path I'm trying to download to is denied. I included a stripped down version of the code I am using below.
var projectCollection = GetProjectCollection();
var versionControl = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));
versionControl.DownloadFile('$/path to file', 'local path to download to');
Does anyone know how to resolve this issue?
I found the issue.
The second argument in DownloadFile() needs to be the file name it will be downloaded as and not the parent directory it will be placed in. I thought it just needed the directory name.
So instead of what I originally had
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp");
it needs to be
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp\\Readme.txt");
This is because the process does not have rights to the local path. Make sure the local path has the appropriate right set to the user that is running the process.

Categories