This is my code broken down:
fileName = string.Concat("C:", @"\test.txt");
StreamWriter sw = new StreamWriter(fileName, false);
StringBuilder row = new StringBuilder();
row.AppendLine("Test");
sw.Write(row.ToString());
sw.Close();
This code does not generate a file, nor does it give an error. If I change the first line to filename = "test.txt";
then the program creates the file in the same location as the exe (bin/debug) and writes the text to it. How can I get the program to write to a different path besides the path the exe is located in?
Write somewhere other than C:\
. Windows does not allow non-admin users to write to the root of the C: drive, and your string.Concat
sets a folder that tries to do so. Change it to a different folder than the root of the drive, and it should work fine.
(You should be using Path.Combine
instead of string.Concat
to build path and filenames, BTW. It properly handles adding path separators in the proper locations as needed.)