How to select a folder relative to root in asp.net website? - c#

I am trying to select all the files from a folder in my website and store them in a collection. The problem is that when I run the website it is not selecting the folder in my website:
This is the basic structure: [Root Folder] --> [FilesFolder]
Here is the code I am using:
DirectoryInfo dir = new DirectoryInfo("FilesFolder");
But it is showing this at runtime as the location of the folder:
C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\FileUploads
Is there a way to select the folder relative to the root of the website?
I am using C# with ASP.NET 3.5

You need to provide the full path to the web site when accessing it through the directory as in:
new DirectoryInfo("c:\inetpub\wwwroot\RootFolder\FilesFolder")
If you are trying to do this within an ASP.NET web site code, you can use Server.MapPath as in:
string path = Server.MapPath("~/FilesFolder");

Use:
Server.MapPath("~/FilesFolder");
More about it here: http://msdn.microsoft.com/en-us/library/ms178116.aspx

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.

ASP.NET MVC and IIS Express - Change executing directory

Currently I am working on an ASP.NET MVC application that should use an existing framework. I am hosting this ASP.NET MVC app in IIS Express. Some classes of this framework assume that files are relative to the current directory. Right now the assemblies are executed in c:\users\MyName\appdata\local\temp\temporary asp.net files\root\3c076611\5261f232\assembly\dl3\d36edef7\e39ad394_8136d101.
Is it possible to change this directory?
var rootLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
now load your file as
var filePath = Path.Combine(rootLocation,"relative path to your assembly");
Edit:
if you cannot change framework, then you can change temp directory using web.config, add <compilation tempDirectory="C:\Project\Temp\">, but this will change only "c:\users\MyName\appdata\local\temp\temporary asp.net files\" part, rest of additional folders will still be there

How to access "project" folder in website (VS API)?

I'm writing a Visual Studio editor plugin. This plugin's functionality includes modifying the project's folder and file structure. I also need access to physical folder where the project is being held. Currently I'm getting the project folder in the following way:
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)serviceProvider.GetService(typeof(EnvDTE.DTE));
var projectItem = dte.Solution.FindProjectItem(pszMkDocument);
var project = projectItem.ContainingProject;
Then I may query System.IO.Path.GetDirectoryName(project.FileNames[0]) and get required path.
But how can I do so in case of website project in Visual Studio? Websites does not have project file as such, and the previous method returns a http:// path instead of local path.
The EnvDTE.Project.Properties collection contains a property named "FullPath" whose Value returns the physical folder for web site projects.
you can use server.mappath
HttpContext.Current.Server.MapPath("~/App_Data/Example.xml");

How .Net names its temp folder inside C:\Windows\Microsoft.NET\Framework\v{version}\Temporary ASP.NET Files\root

I have some websites on my IIS machine. Lets say WebsiteA, WebsiteB and WebsiteC.
Inside .Net temp folder
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\{folderName}
each of the 3 website will have their own folder with some random name.
For example:
WebsiteA is using a folder named 5a3e1z1j. This folder will have the following path :
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\5a3e1z1j
WebsiteB is using a folder named b51o8881. This folder will have the following path :
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\b51o8881
WebsiteC is using a folder named ad4966a9. This folder will have the following path:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ad4966a9
How do I know which website is using which folder? So while the process of that website is still running, it won't allow me to delete the folder. It will only allow to delete the folder if I kill the process / stop the application pool or IIS.
Then I did some testing, what if I delete the folder (of course while there is no running process), what will happen to the site.
So apparently it will create a new folder with same exact random name.
Then I come up with a conclusion that this name is stored somewhere, so even though the folder is deleted. It will be recreated back with the same name.
My question is "How .Net create the name of these .Net temp folder so that for each website will have a consistent folder name? If it's stored somewhere after a successful build of the website, where can I find it?"
What I am trying to achieve is I want to create a tool that will do application pool restart if it's given input of website name. And also at the same time delete the .Net temp folder. But only the one associated to the input website.
If something is not clear, please let me know.
Thanks

Were would the appropriate location to store files in a website? Like a temp file?

I have a MVC application that downloads a file and then has to unzip it and use the data.
Where would the correct location be to store this file?
If I were using a windows Forms application I would store it in the bin and use the following to get the path. I'm trying to do the same for a web application?
String path = Application.StartupPath;
Create a folder in the root of your project and call it 'Temp' or something similar.
Store the downloaded files in it.
I think the best place would be to create a folder outside the root folder of the application so that this folder won't be accessible by website users.
To access this folder you have mainly two options:
create a virtual directory on the IIS level and access it.
Use AspEnableParentPaths, here is an example: http://msdn.microsoft.com/en-us/library/ms524697%28v=vs.90%29.aspx

Categories

Resources