Home c# How to register the path to the TXT file?

How to register the path to the TXT file?

Author

Date

Category

How to specify the path to the file that is in the same directory as the project itself, that is, so that this file can and project can be transferred to another place and did it all still find this file without changing the path to it? FileStream F = New Filestream (@ "C: \ users \ user \ desktop \ taskone \ input.txt", filemode.open, FileAccess.Read);


Answer 1, Authority 100%

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

  • Working Directory
  • Output Path

Take for example a draft console application.

Working Directory option determines which operating directory will be set by default to the current console application process.
If this parameter is empty, the working directory will be the directory of which runs the EXE file. And its place is determined by the project parameter Output Path :

Default is the subdirectory of the current project Bin \ debug.

Thus, in this case, to contact the input.txt file located in the project directory you need to contact the two directory above, i.e. Use “..” in the path path:

FileStream F = New Filestream (@ ".. \ .. \ input.txt", FileMode.Open, FileAccess .Read);

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

The project directory is the directory in which the project file is .csproj .


Answer 2

Enable the file to the project (if this is not yet done) and in the properties, specify it to be copied to the output directory. For the English Studio, it will look like this (and everything is clear in Russian):

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

filestream f = new filestream ("input.txt", filemode.open, fileaccess.read);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions