✅ 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).
26 Replies
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
ahhh
and not seeing the changes u think its not writing to it?
yeah true
yeahh
I think it's in the debug folder wait
need to find it
bin\Debug\net9.0-windows
it will be something like this
yep
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 itoh 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 ?
it depends on what the purpose of the file is
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 ?
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
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
yep I see
ahh saw the Path.Combine method earlier, this is where it is used
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
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 ?GetFolderPath is a specific method to get the user special paths
if you want to write your own path u dont need that

https://learn.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=net-9.0
and here u can see the list of options it can take https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-9.0
Yep I see, I will have a look, ty !
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
But it isn't recommended, the following also:
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?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
Yep I see, I believed my doubts have been cleared 👍
$close
If you have no further questions, please use /close to mark the forum thread as answered