Could not found the path for create and write file - c#

My code is
after run on Azure it say:
{
"Message": "Cannot create 'D:\\home\\site\\wwwroot\\DataFile' because a file or directory with the same name already exists."
}
how to resolve this??? My code tells it if the directory exists will be ignored and pass not created...why does it try to create and tell can not create because the directory exists???
Work in local work well...no error...the problem appears when run in azure
If I remove the code create a directory, It will error that the path does not exist and it doesn't create and write file...
In Azure
In Local

This code work OK
In Azure it cause error be cause in D:\home\site\wwwroot\DataFile have a DataFile, it is a file,
I use the Kudu, (In App Service in Azure, search Advanced Tools)
then remove the file DataFile, then all things OK
Thanks all for support me!

Related

In azure web app File.Exits return false actually file exists

We have a mail template in ~/Content/EmailTemplate/template.cshtml. But whenever we do the following:
var path="D:/site/wwwroot/Content/EmailTemplate/template.cshtml"
File.Exist(path)
File.Exist(path) returns false. While debugging in local source it works fine. It returns false in azure web app only. I have checked the file already exists there.
If you move the above code to another environment it will break.
I would suggest that you use map path as this will assure that you can move from hosting environment to hosting environment, including your local development environment.
string path = Server.MapPath("~/path/tofile");
You will also have higher confidence that you are targeting the file correctly.
For Azure WebApp,
D:\home is shared for us and we could read or write file in this path. More detail about Home directory access please refer to the WebApp Sandbox. File structure on Azure please refer to another document. We could browse it from Kudu (http://yourwebsite.scm.azurewebsites.net/) tool. In your case, we also could use the following code in the Azure WebApp.
string path = Environment.GetEnvironmentVariable("HOME") + #"\site\wwwroot\Content\EmailTemplate\template.cshtml"

Server.MapPath Access is denied On Azure

I am exporting an excel using c# code and spreadsheetlight dll.
I am trying to insert a picture into excel, everything works well on local machine but on live web site it gives following error
Access to the path 'F:\sitesroot\0\Images\Logo.png' is denied
My c# code.
string filePath = Server.MapPath("~/Images/Logo.png");
SLPicture pic = new SLPicture(filePath); //SLPicture - An inbuilt class for inserting image
Currently what I am doing is manually providing access rights on IIS server but which is not the correct way to go since each time I upload the service I have to redo that again and again
Any expert advice - How I can provide access right through code?
Or How to get rid of this error.
Thanks.
Assuming you are using Cloud services...
You are supposed to use Local Storage for this (http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx).
Basically;
In your Cloud project, go to the properties for the Role and select Local Storage.
Add a new local storage and give it a name, i.e. "foo" and a maximum size. If this is for temporary processing rather than caching, you probably want to tick "Clean on role recycle".
In your code call RoleEnvironment.GetLocalResource("foo") to get the folder name you can use and then do something like Path.Combine("path","Logo.png") to get a file name which you can then write to.

Access to the path 'xxx' is denied (where 'xxx' is a folder)

I know this has been asked before many times, but I have browsed tens of similar questions without help.
This error is showed when my asp.net 4.0 application tries to access a folder on my local drive, doing this:
XmlTextReader confReader = new XmlTextReader (filename);
while (confReader.Read()) // <- error line
{
// do something
}
In my web.config I have <Identity Impersonate="true">. For the folder I have assigned full-access to:
everyone
IIS APPPOOL\<custom apppoolname>
NETWORK SERVICE
Debugging the application, I can put up a watch which evaluates this call right before the incriminated line:
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
the call shows the current user is IIS APPPOOL\<custom apppoolname>.
I'm on Windows 7 64bit with Sp1. I know it's probably something stupid but I have already spent an awful amount of time on this.
Edit:
The problem was in the variable "filename", which pointed to the right folder, but for a configuration problem the filename was missing (i.e. the content was "c:\data\" instead of "c:\data\file.xml"). The Asp.net error was actually confusing, since I had all the right permissions to access that folder. DJKRAZE pointed me to the right direction.
Here is what I would suggest checking
where are you declaring filename..?
Does the file even exist..?
do you have rights to that folder..? have you tried running VS as Admin..? do you have Virtual Directory setup for the web app..sounds like you have a few things configured improperly as well but can't really tell based on the code for starters this line System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
should yield your domain\\username or compuername\\username
Glad that FileName was all it was
IIS requires the path to a folder in the server machine has rights for Anonymous user login to access.
Make sure the user Anonymous has the rights to access the folder.

Writing to a file I get unauthorized access error.How can I do it?

Despite numerous post on the web I cannot find an answer to my problem.
I am writing an application that writes csv files to folders.Users should be able to pick a directory.
I am developing in windows 7 using vs2010 running my app in Admin Mode.Regardless of all this I still get the "Unauthorized access exception" when I do
var path=#"c:\" or c:\MyFolder
StringBuilder sb=new StringBuilder();
sb.AppendLine("Test");
var myFile=sb.ToString();
using (var writer=new StreamWriter(path))
{
writer.Write(myFile);
}
Am I missing something?
I have feeling that in window7 you can only write to designated folders.Is this what's happening?
any suggestions?
EDITED
I have created few folders under "C:\MyFolder\"
I am not using any credentials eg windows impersonation etc..
It does write if it writes to the bin\debug\ of my class library. but not to any designated folder.
Is your code snippet the real code causing the problem?
On the face of it, you are trying to stream the text "Test" into a directory on the file system, not trying to write a file. (path is just assigned to #"C:\"). I'm not surprised that you get an UnauthorizedAccessException.
Assign the full path of the file you want to write into your path variable, and I imagine you'll succeed.
Try running your app with "Run as Administrator". The comments above will probably also steer you in the right direction. You should definitely pick a directory that your windows users has access to edit.

Access to the path is denied

I'm trying to use a ASP.NET(C#) application on a IIS-Server. Everything is working great, i have only one problem. I'm trying to create a CSV-File in the 'C:\inetpub\wwwroot\bin' directory, but i get the errormessage "Access to the path 'C:\inetpub\wwwroot\bin' is denied.".
I tried to give the 'IUSR' and the 'NT-AUTHORITY/networkservice' all rights, but it still doesn't work. I also tried it in an other directory but also doesn't work.
OS: Windows Server 2008 R2
Thanks in advance!
Edit: Thx! Works now.
use the App_Data folder for that, NOT the bin folder:
more here: http://msdn.microsoft.com/en-us/library/t990ks23.aspx
and: http://msdn.microsoft.com/en-us/library/ex526337.aspx
Try adding the rights to the ASPNET user, that might be the issue.
Also, writing a file at the root of the web-folder, is basically a bad idea.
It's much safer if you put create the files in a folder that is not accessible from the web, and than use a separate page to download the file (I'm guessing that's what you are trying to accomplish), something along these lines.

Categories