C
C#4mo ago
Logan

noob question

I'm trying to do some basic file handling for some college coursework, the file.create line doesn't work and I can't figure out why. the createdirectory works when the file.exists returns false but if it returns true nothing happens
No description
36 Replies
Angius
Angius4mo ago
Just in case you're running into issues with path separators, try using Path.Join() instead of string interpolation to create your path strings
Logan
LoganOP4mo ago
I'll give that a go Nothing happened again No error, just nothing happens
Angius
Angius4mo ago
What's your current code?
Logan
LoganOP4mo ago
No description
Logan
LoganOP4mo ago
I've figured out that it doesn't work because file.exists never returns true but I'm not sure why that is
Angius
Angius4mo ago
You used Path.Join() in one place, cool What about the other place?
Logan
LoganOP4mo ago
where? oh wait yeah hold on Nevermind when it returns false it doesn't do anything there either
Angius
Angius4mo ago
Also, File.Exists() checks if a file exists
Logan
LoganOP4mo ago
is there a directory exists?
Angius
Angius4mo ago
What userdir path is is a directory Yep
Logan
LoganOP4mo ago
ahhh that would be why then Wait hold on I don't even need the if statement anyway if it creates the directory both times unless if I create the directory after it's made will it delete other files than yes.txt? I got it working, the path.join works too, thanks man You're a life saver
Angius
Angius4mo ago
Anytime :Ok:
Logan
LoganOP4mo ago
I have another question now lol
Logan
LoganOP4mo ago
how do I append the password to the user's file?
No description
Angius
Angius4mo ago
Does File.AppendAllText() not work?
Logan
LoganOP4mo ago
it doesn't
Angius
Angius4mo ago
What does it do?
Logan
LoganOP4mo ago
Nothing
Angius
Angius4mo ago
And are you sure password is not, for example, empty?
Logan
LoganOP4mo ago
I actually think it is but I can't figure out why
Angius
Angius4mo ago
Whatever GetNode<T>() does, the Text of it is empty
Logan
LoganOP4mo ago
I know that part but I don't know why it's empty I'm using c# in godot btw so the getnode is getting the text from a text input node Nvm it's not empty for some reason the append just isn't happening
Angius
Angius4mo ago
File.Create() returns a FileStream instance, as per documentation: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.create?view=net-8.0
Logan
LoganOP4mo ago
No description
Logan
LoganOP4mo ago
i've changed it to this now it creates the file but still nothing being written to the file
Angius
Angius4mo ago
The streamwriter should be taking the user as an input Since that is the stream you want to write to
Logan
LoganOP4mo ago
this is one of the cons of copypasting code from stack overflow What would that look like?
Angius
Angius4mo ago
new StreamWriter(user)...?
Logan
LoganOP4mo ago
ahhh
Angius
Angius4mo ago
Alternatively... you just want to create a new file with some text, right? Not append text to an existing file?
Logan
LoganOP4mo ago
Yeah
Angius
Angius4mo ago
If so, then maybe just File.WriteAllText() would do No need to fiddle with streams and what not
Logan
LoganOP4mo ago
True I feel like my college should've probably taught us c# before telling us to make an entire game in c# in less than a year
Angius
Angius4mo ago
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-8.0
Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is truncated and overwritten.
Logan
LoganOP4mo ago
That works thanks fam
Angius
Angius4mo ago
:Ok:
Want results from more Discord servers?
Add your server