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
36 Replies
Just in case you're running into issues with path separators, try using
Path.Join()
instead of string interpolation to create your path stringsI'll give that a go
Nothing happened again
No error, just nothing happens
What's your current code?
I've figured out that it doesn't work because file.exists never returns true
but I'm not sure why that is
You used
Path.Join()
in one place, cool
What about the other place?where?
oh wait yeah hold on
Nevermind when it returns false it doesn't do anything there either
Also,
File.Exists()
checks if a file existsis there a directory exists?
What
userdir
path is is a directory
Yepahhh
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
Anytime :Ok:
I have another question now lol
how do I append the password to the user's file?
Does
File.AppendAllText()
not work?it doesn't
What does it do?
Nothing
And are you sure
password
is not, for example, empty?I actually think it is
but I can't figure out why
Whatever
GetNode<T>()
does, the Text
of it is emptyI 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
File.Create()
returns a FileStream
instance, as per documentation: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.create?view=net-8.0i've changed it to this now
it creates the file but still nothing being written to the file
The streamwriter should be taking the
user
as an input
Since that is the stream you want to write tothis is one of the cons of copypasting code from stack overflow
What would that look like?
new StreamWriter(user)
...?ahhh
Alternatively... you just want to create a new file with some text, right? Not append text to an existing file?
Yeah
If so, then maybe just
File.WriteAllText()
would do
No need to fiddle with streams and what notTrue
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
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.
That works
thanks fam
:Ok: