How to get the file path in Azure app Service resource? - c#

I am writing C# code and I am trying to put the relative path to a file containing SQL code and execute it programmatically.
My application is an Azure App Service resource.
How can I get the path of the file and where does it exist?
Thanks

You can check the files deployed with your project in the Azure console.
On the path D:\home\site\wwwroot you would find the same files as in the bin\Release\net6.0 folder.

Related

System IO exception even though file is added in Visual studio C#

I have a project into which I added a JSON file, but when I try to read the file in code I get this error.
Could not find file 'C:\\Program Files (x86)\\IIS Express\\client_secret.json'.
"ExceptionType": "System.IO.FileNotFoundException"
I have the file added in like this.
I can access it when I copy the full path and then read it.
its Asp.NEt or WEBAPI, i.e. its Web application you can access path by using Server.MapPath method.
Example : Server.MapPath("~/script/data.txt")//this locate file in your script folder on sever
for the folder under than website you need to do like this
Server.MapPath(~/client._secret.json) //here ~ sign means relative path from root
Server.MapPath method gives you physical path of your file on server machine.

How to access a folder in c# in application files which is created after publishing application using relative path?

I am creating a application where I need to access images from a image folder in application files which I will add later after publishing the application. But the thing is when I access my application data path it is .../AppData/Temp...
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
So what is the way to access the data in Application Files in .exe directory.
My base directory is in Temp. But I want to access the one from where I executed the .exe
See more How can I get the application's path in a .NET console application?
Sulotion:
Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location,#"/Images/logo.png");
I hope this helps you.

Creation of Text file using web service in same directory using C#.net

I want to create a text file using C#.net in the same directory in which the file reside.
what is the solution?
When i am trying to create i got error "Access to the path 'C:\Windows\SysWOW64\inetsrv\test.txt' is denied". please help.
Thanks
You are not trying to create the file in the same directory as is the web service directory, because when you create for example new StreamWriter("test.txt"), it creates the file in the current working directory of the process (ASP.NET / IIS).
Use Server.MapPath('~\test.text')
This can be caused by windows permissions where you are not allowed to create files on your 'c' drive. Or a folder has certain access rights assigned to them. Try changing the permissions or right to another file to find out.

C# + File.OpenRead(path_to_file)

I am developing a HTML5 based WebApp being hosted in IIS7. This webapp sends requests to webservices being hosted in IIS7.
The service initialization looks up for a specific file e.g: "appfile.txt" as
FileStream stream = File.OpenRead("appfile.txt"); // opens file for reading.
This call when run as a console application looks up in the project\bin or output directory and able to locate the specified file.
But the same hosted in IIS7 looks up in "C:\windows\system32\inetsrv\appfile.txt".
Are there any configuration item having used in web.config locates the file from the Bin directory of the IIS7 application and not anywhere else?
Any help is much appreciated.
If the app file is in your web application folder, try using Server.MapPath to get The location of the file relative to the root of the web app:
File.OpenRead(Server.MapPath("~/appfile.txt"))
That should work. You probably need to set up the appfile.txt properties so it is copied to the output folder.
You can certainly create your own section in the web.config file to grab the file path that you want. It doesn't need to have been predefined. Then just use any XML reader you want.
Alternatively, you can make a .resx file very easily in Visual Studio and just populate the path there as a variable.
Thanks for all your valuable comments, I could resolve this issue myself using the following code snippets
string path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
string[] labs = File.ReadAllLines(path + "/appfile.txt");
This bit of code allows me to read the file contents without any issues.

c# WCF Server application - access to a file on the server relatively

I have a c# server application (WCF) and I have a file saved on that server and i want to access it relatively so every dev machine can work with it.
this is the file path.
C:\Users\ben\Documents\Visual Studio 2010\Projects\ myfile.xml
the project where i want to access the file from is in:
C:\Users\ben\Documents\Visual Studio 2010\Projects\ MyProject
what is the best way to access the myfile.xml (relatively)? from MyProject?
Well, when you run the project, your current directory will be something like:
C:\Users\ben\Documents\Visual Studio 2010\Projects\MyProject\MyProject\bin\Debug\
So you will probably want to do something else.
Include the file in the project and go to File Properties for the file. Select Copy Always for the Copy to Output Directory setting. The file will then be copied to the same directory as the EXE when compiling/building. That way you can access the file simply by its filename.
From your question , what is under stood is that you want to access your project location , without the executable file and folder . To do that try the following code :
string AppPath = System.Environment.CommandLine;
int pos = AppPath.IndexOf("bin");
AppPath = AppPath.Remove(pos);
AppPath += "myfile.xml";

Categories

Resources