Getting path to parent folder of solution file using C #

I start with C # and I have a folder from which I am reading a file.

I want to read the file that is in the parent folder of the solution file. How to do it?

string path = ""; StreamReader sr = new StreamReader(path); 

So, if my XXX.sln file is located in C:\X0\A\XXX\ , then read the .txt files in C:\X0\A\ .

+6
source share
5 answers

Try the following:

 string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName,"abc.txt"); // Read the file as one string. string text = System.IO.File.ReadAllText(startupPath); 
+12
source

It would be a mistake if I believe your application relied on the location of the file based on the relationship between the file path and the solution path. Although your program can run at Solution/Project/Bin/$(ConfigurationName)/$(TargetFileName) , this only works when you perform actions within Visual Studio. Outside of Visual Studio, in other scenarios, this is not necessarily the case.

I see two options:

  • Include the file as part of your project and in its properties copy it to the output folder. Then you can access the file:

     string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Yourfile.txt"); 

    Please note that during deployment, you need to make sure that this file is also deployed along with your executable file.

  • Use command line arguments to indicate the absolute path to the file at startup. This can be used by default in Visual Studio (see "Project Properties -> Debug Tab ->" Command Line Arguments "). For example:

     filePath="C:\myDevFolder\myFile.txt" 

    There are several ways and libraries regarding command line parsing. Here is the answer to stack overflow when parsing command line arguments.

+6
source

You may like this more general solution, which depends on the search for the *.sln solution file, scanning all the parent directories from the current or selected one, at the same time closing the case of finding the solution catalog!

 public static class VisualStudioProvider { public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null) { var directory = new DirectoryInfo( currentPath ?? Directory.GetCurrentDirectory()); while (directory != null && !directory.GetFiles("*.sln").Any()) { directory = directory.Parent; } return directory; } } 

Using:

 // get directory var directory = VisualStudioProvider.TryGetSolutionDirectoryInfo(); // if directory found if (directory != null) { Console.WriteLine(directory.FullName); } 

In your case:

 // resolve file path var filePath = Path.Combine( VisualStudioProvider.TryGetSolutionDirectoryInfo() .Parent.FullName, "filename.ext"); // usage file StreamReader reader = new StreamReader(filePath); 

Enjoy it!

Now a warning .. Your application should be an agnostic solution - if this is not a personal project for any decision processing tool, I would not mind. Understand that your application, once distributed to users , will be in a folder without a solution. Now you can use the "anchor" file. For instance. search for parent folders like me and check for empty app.anchor or mySuperSpecificFileNameToRead.ext ; P If you want me to write a way that I can, just let me know.

Now you really will like it !: D

+4
source

I think this is what you want. Not sure if this is a good idea when posting:

 string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; 

Requires using System.IO;

+1
source
 string path = Application.StartupPath; 
0
source

Source: https://habr.com/ru/post/954575/


All Articles