C
C#2w ago
Faker

✅ StreamWriter not writing to file

Hello guys, I changed my file properties to "Copy if newer" (I didn't understand why we need to do that, would really appreciate if someone can explain) .... I was able to read a file but when it comes to write to it, it seems that nothing is being written and no exception is being thrown (I added the .close method).
C#
// How to read and write to file in C#
string filePath = Path.GetFullPath("sample.txt");

try
{
var sr = new StreamReader(filePath);
var line = sr.ReadLine();
Console.WriteLine(line);
sr.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

try
{
using (var sw = new StreamWriter(filePath, true, Encoding.Unicode))
{
sw.WriteLine("Just a new line");
sw.WriteLine("Just a new line 2");
}

}
catch (Exception e)
{
Console.WriteLine($"Processing failed: {e.Message}");
}
finally
{
Console.WriteLine("Finally block executed");
}
C#
// How to read and write to file in C#
string filePath = Path.GetFullPath("sample.txt");

try
{
var sr = new StreamReader(filePath);
var line = sr.ReadLine();
Console.WriteLine(line);
sr.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

try
{
using (var sw = new StreamWriter(filePath, true, Encoding.Unicode))
{
sw.WriteLine("Just a new line");
sw.WriteLine("Just a new line 2");
}

}
catch (Exception e)
{
Console.WriteLine($"Processing failed: {e.Message}");
}
finally
{
Console.WriteLine("Finally block executed");
}
26 Replies
leowest
leowest2w ago
when you run your app it will create a folder and all files that are in your project set to copy will be copied over to that folder so when you read the file sample.txt you're reading that copy and when u append text you're also appending it to that copy not the file in your project so maybe you're checking the sile in your project
Faker
FakerOP2w ago
ahhh
leowest
leowest2w ago
and not seeing the changes u think its not writing to it?
Faker
FakerOP2w ago
yeah true yeahh I think it's in the debug folder wait need to find it
leowest
leowest2w ago
bin\Debug\net9.0-windows it will be something like this
Faker
FakerOP2w ago
yep
leowest
leowest2w ago
so to explain what copy if newer does it means that it will only copy to the directory if the file that exists there differs from the one u have in your project i.e.: if u add things or remove things from it
Faker
FakerOP2w ago
oh ok, I see, yeah you were right, I can now see the new lines being appended hmm but basically, we would need to reflect those changes back to the file in our project, no ? well here I'm just experimenting but in production etc ?
leowest
leowest2w ago
it depends on what the purpose of the file is
Faker
FakerOP2w ago
ah yeah true, say I would need to have the new contents back in the file in the project, I would need to change from debug mode to release mode, something like that ?
leowest
leowest2w ago
let's say you're learning and you're using it as means to store username and password so yeah u would want to have a common place to store that file so it can be accessible in both scenarios so if u wanted to access the sample.txt in your project u would need to go back a few folders in the path ur giving it as one example
../ = bin
../../ = bin/debug
../../../ = bin/debug/.net9-windows
../ = bin
../../ = bin/debug
../../../ = bin/debug/.net9-windows
but these kind of things however can cause problems if u plan on distributing the app for example because it might not be true that u can find that file in the other target systems by using that path so what is usually recommended is a common folder to store the file such as AppData/roaming for example
Faker
FakerOP2w ago
yep I see
leowest
leowest2w ago
var myAppData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppName");
var myAppData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppName");
Faker
FakerOP2w ago
ahh saw the Path.Combine method earlier, this is where it is used
leowest
leowest2w ago
yes its a nice way to merge multiple paths without having to worry if you're using the proper separator for folders which changes depending on the OS i.e.: c:\a\myfile.txt /home/a/myfile.txt
Faker
FakerOP2w ago
yep I see I will just experiment a bit and came back if I have other doubts, thanks !! last question, when we write the GetFolderPath method, we use system folders, like MyDocuments or Desktop or ApplicationData.... can we use a custom folder, like one in our project itself ?
leowest
leowest2w ago
GetFolderPath is a specific method to get the user special paths if you want to write your own path u dont need that
leowest
leowest2w ago
No description
Faker
FakerOP2w ago
Yep I see, I will have a look, ty !
leowest
leowest2w ago
var myCustomPath = Path.Combine("Data", "sample.txt");
var myCustomPath = Path.Combine("Data", "sample.txt");
Faker
FakerOP2w ago
yeah it worked, I understand what my current working directory was now, why it wasn't working etc, thanks !! By the way, you mentioned earlier, we could do something like
../ = bin
../../ = bin/debug
../../../ = bin/debug/.net9-windows
../ = bin
../../ = bin/debug
../../../ = bin/debug/.net9-windows
But it isn't recommended, the following also:
C#
string workingDirectory = Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
string filePath = Path.Combine(projectDirectory, "Doc/sample.txt");
C#
string workingDirectory = Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
string filePath = Path.Combine(projectDirectory, "Doc/sample.txt");
Isn't recommended because of the file separator since it depends on OS being used; that's why we tend to use the GetFolderPath method?
leowest
leowest2w ago
no Environment.GetFolderPath is a special method specifically for what I described above it has nothing to do with anything else this is not recommended because there is no guarantee that the app u send me will have files in those directories imagine I put your app in c:\ so its at c:\yourApp how would ..\..\..\ work? if there are no parents this is called relative path when you try to access something by going down or up levels
Faker
FakerOP2w ago
Yep I see, I believed my doubts have been cleared 👍
leowest
leowest2w ago
$close
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?