2

I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.

using (StreamWriter file = File.AppendText("log1.txt"))
{
       file.WriteLine("Hello from the text file");
}

If the file does not exist, the application creates it in the autogenerated folder bin/Debug. Is there a way to create this file in the project's directory, where I have .csproj file?

And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?

Mihai Socaciu
  • 155
  • 2
  • 12
  • 2
    In real-world scenarios, you publish your application, you take the content (a bunch of dll files and other things) and you deploy them wherever you want. That means you won't have bin nor debug folders. What you're trying to do now will ONLY work when debugging the application using Visual Studio or using `dotnet run`. – Haytam Aug 24 '19 at 13:40
  • 1
    Files are created in bin/debug because this is the current working directory. When you build the solution, that's where your executable and dlls are deployed, in debug mode – Cid Aug 24 '19 at 13:44
  • Please remember: on Windows a process has a startup directory (where the executable resides) and a working directory. By default, these are the same, but it depends on how you start the process. If no path is given, a file name in such case is resolved in the working directory (assembly resolution has its own mechanism). You have some other special folders at hands, but you need to use them explicitely. – ZorgoZ Aug 24 '19 at 13:45

3 Answers3

1

Is there a way to create this file in the project's directory, where I have .csproj file?

Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.

And more important, in real-world applications, when you work with files, you keep them in bin/Debug?

No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:

  • User Data
  • Roaming User Data
  • All User Data

You can acess the above folders in .NET application using the Environment.SpecialFolder:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

As per your given code, try this :

var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
                              "log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
    file.WriteLine("Hello from the text file");
}

This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.

That's why .NET creates them there firstly?

If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug

Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
0

Is there a way to create this file in the project's directory, where I have .csproj file?

Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.

And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?

That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.

andresantacruz
  • 1,676
  • 10
  • 17
0

You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.

If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79