1

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.

madvora
  • 1,717
  • 7
  • 34
  • 49

2 Answers2

0

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.

Community
  • 1
  • 1
Matyas
  • 1,122
  • 5
  • 23
  • 29
  • Not sure I'm getting this. I added some extra information in my question. The file I'm trying to get to is an exe, so I don't think that's available for the Copy to output directory setting. Not too familiar with this process. – madvora Sep 13 '15 at 17:30
  • 1
    Are you *absolutely* sure that you need to call the console application? Can't you just use it's interface and call some method in it? This is rather strange... Especially if my crystal ball is right and what you are calling is the another project in your solution. I such a case it would be probably enough to add a [reference](https://msdn.microsoft.com/en-us/library/f3st0d45.aspx) to the project and call it directly. – Matyas Sep 13 '15 at 17:35
  • Yes I will need to call the console application. Basically this is a coding exercise. I'm just demonstrating that I can call a web api from the Home/Index page (on button click) then clicking another button opens my console app which calls the same web api. If you can explain your reference comment at the end I would appreciate it, or any solution for opening up my console app exe from a button click on a web page (same solution, different projects) – madvora Sep 13 '15 at 18:28
  • @madvora check this out: http://www.c-sharpcorner.com/uploadfile/61b832/creating-class-library-in-visual-C-Sharp/ – Matyas Sep 13 '15 at 19:32
0

To set a reference path

  1. In Solution Explorer, select the project.

  2. On the Project menu, click Properties.

  3. Click Reference Paths.

  4. In the Folder text box, specify the path of the folder that contains assemblies. To browse to the folder, click the ellipsis (…).

  5. Click Add Folder.

To overwrite a reference path

  1. In Solution Explorer, select the project.

  2. On the Project menu, click Properties.

  3. Click Reference Paths.

  4. In the Reference Path box, select the path to overwrite.

  5. In the Folder text box, specify the path of the folder that contains assemblies. To browse to the path, click the ellipsis (…).

  6. 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

  1. In Solution Explorer, select the project.

  2. On the Project menu, click Properties.

  3. Click Reference Paths.

  4. In the Reference Path box, select the path to remove.

  5. Click Remove.

Matyas
  • 1,122
  • 5
  • 23
  • 29