How to specify the path to the file that is in the same directory as the project itself, that is, so that this file and project can be transferred to another location and he still found this file without changing the path to it? FileStream f = new FileStream(@"C:\Users\User\Desktop\TaskOne\input.txt", FileMode.Open, FileAccess.Read);

  • one
    project or all the same executable file? - Grundy
  • Folder with the project in which the file is located. - Denis
  • 2
    you can get the path to the executable and the path of the current folder, the project is in no way associated with the final executable file - Grundy

2 answers 2

For this you need to operate with two parameters of the Visual Studio project:

  • Working directory
  • Output path

Take for example the project console application.

enter image description here

The Working directory parameter determines which working directory is set by default to the current process of the console application.
If this parameter is empty, then the working directory will be the directory from which the exe file is running. And its place of creation is determined by the parameter of the project Output path :

enter image description here

By default, this is the subdirectory of the current project bin \ Debug.

Thus, in this case, to access the file input.txt located in the project directory, you need to go to two directories above, i.e. use ".." in the file path:

 FileStream f = new FileStream(@"..\..\input.txt", FileMode.Open, FileAccess.Read); 

Since the paths are all specified relative to the project directory, you will not need to change the paths when moving the project folder.

The project directory is the directory where the .csproj project file is .csproj .

  • one
    Started for health, finished for the rest. Well, you can not access the file ".. \ .. \ input.txt", during operation, this path will indicate an unknown way! - Pavel Mayorov

Include the file in the project (if it is not already done) and specify in the properties that it be copied to the output directory. For an English studio, it will look like this (and in Russian everything is clear):

In this case, with the default settings, the program will be able to find it in the current directory:

 FileStream f = new FileStream("input.txt", FileMode.Open, FileAccess.Read); 
  • Only when starting from the command line or some other FAR or script, this file may not be found. In order to work exactly as you wrote, you also need to assign the path to the working folder to the equivalent path to the executable file. - rdorn
  • @rdorn here it all depends on what kind of file. If this is an internal file for a program, then yes, we should look for it relative to the executable file. But the file name tells me that this is the input to the program. In this case, the search in the current directory is no longer a bug, but a feature - the user will most likely create this file where it is convenient for him, and not where the program is located. - Pavel Mayorov
  • @rdorn here, in an amicable way, it would be necessary to take another file name as a launch argument - but this is already the next level. - Pavel Mayorov