In another project, I wanted to point to a file in the App_Data folder of my project. So in Web.Config I added this.
<appSettings>
<add key="filePath" value= "App_Data/MyFile.xml" />
</appSettings>
Then in my controller, I used this to get access to the file.
string relativePath = ConfigurationManager.AppSettings["filePath"];
string xmlData = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
That works great, but now, I need to get to a file in another project, in the same solution .
BaseDirectory points to the project folder I'm currently in. What can I do to get one level up from there? Is there a way I can just get the path of the solution?
Further Information
To be more clear, I want to open a console app from my web app. Right now I'm doing that calling a controller that gets the hardcoded path to the exe like this. I need this path to be relative instead.
Process process = new Process();
//Path of the file
process.StartInfo.FileName = "C:\\Users\\MyName\\Documents\\Visual Studio 2015\\Projects\\SolutionName\\ConsoleApp\\bin\\Debug\\ConsoleApp.exe";
process.Start();
UPDATE
Rather than go into another project, I just changed the output path of the console application to the bin/Debug folder of the WebApp project. That way I can stay in the same project folders and reach the console app with the same relative path stuff I used before.
Firstly I would use application settings and not the appSettings
, which than need some "magic string" ( ConfigurationManager.AppSettings["filePath"];
) to access. Application Settings are used in the code like this Properties.Settings.Default.SettingName
, they are strongly typed and also stored in application configuration file.
Secondly you can access files from any project like that. The question is, how to get the files to the output directory.
That is done by Build action
and Copy To Output Directory
in the properties of the file in Visual Studio. The other project mus be referenced by the "primary" one.
Also if the projects is more like a resource you can use Resources
to store it and avoid accessing files on the drive directly.
To set a reference path
In Solution Explorer, select the project.
On the Project menu, click Properties.
Click Reference Paths.
In the Folder text box, specify the path of the folder that contains assemblies. To browse to the folder, click the ellipsis (…).
Click Add Folder.
To overwrite a reference path
In Solution Explorer, select the project.
On the Project menu, click Properties.
Click Reference Paths.
In the Reference Path box, select the path to overwrite.
In the Folder text box, specify the path of the folder that contains assemblies. To browse to the path, click the ellipsis (…).
Click Update. The path selected in the Reference Path box is overwritten with the path specified in the Folder text box.
To remove a reference path
In Solution Explorer, select the project.
On the Project menu, click Properties.
Click Reference Paths.
In the Reference Path box, select the path to remove.
Click Remove.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.