Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?
You are not logged in.
Pages: 1
Topic closed
I'm trying to create a new text file and write text to it but I get an error saying something like
This file cannot be accessed because it is being used by another process.
How do I make it to where it is not longer being used by the other process.
Also: Here is the code i'm currently using:
SaveFileDialog saveNote = new SaveFileDialog();
saveNote.Filter = "Supported Files|*.note;*.n;*.txt|All Files|*.*";
saveNote.FilterIndex = 1;
saveNote.Title = "Save your note.";
saveNote.ShowDialog();
if (saveNote.FileName != null)
{
if (!File.Exists(saveNote.FileName))
{
File.Create(saveNote.FileName);
File.WriteAllText(saveNote.FileName, notesyPad.Text);
}
else if (File.Exists(saveNote.FileName))
{
File.WriteAllText(saveNote.FileName, notesyPad.Text);
}
}
Thanks.
Offline
You can't have the file opened... Close it then try again.
thanks zoey aaaaaaaaaaaand thanks latif for the avatar
Offline
You can't have the file opened... Close it then try again.
I don't have the file opened. After creating the file i'm trying to write to it, how do I make my program stop accessing the file so it can then write to it?
Offline
File.WriteAllText doesn't need a created file. It can take care of itself.
Also, xfrogman43 is correct, apparent or not.
File.Create() is different from the other functions in that it does something rather peculiar. It's not actually creating a file. It's creating a STREAM. The stream is causing the IO exception because it has the rights to the file. Sadly, it longs for the day to be disposed of properly. This code doesn't do that.
So if you wanted, you'd call
File.Create(saveNote.FileName).Dispose();
which would be redundant.
Those who can't do, teach.
A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
It is a well-known fact that those people who must want to rule people are, ipso facto, those least suited to do it... anyone who is capable of getting themselves made President should on no account be allowed to do the job.
I think fish is nice, but then I think that rain is wet, so who am I to judge?
For a moment, nothing happened. Then, after a second or so, nothing continued to happen.
Offline
File.WriteAllText doesn't need a created file. It can take care of itself.
Also, xfrogman43 is correct, apparent or not.
Thanks, never knew that!
I know he is correct but I was saying that it wasn't open in some other program (that isn't VS/My Program).
Offline
eeisold wrote:File.WriteAllText doesn't need a created file. It can take care of itself.
Also, xfrogman43 is correct, apparent or not.
Thanks, never knew that!
I know he is correct but I was saying that it wasn't open in some other program (that isn't VS/My Program).
Never read the code, its just that I have had that error before, closed notepad/whatever and it works fine.
thanks zoey aaaaaaaaaaaand thanks latif for the avatar
Offline
Locked on request of OP.
"Sometimes failing a leap of faith is better than inching forward"
- ShinsukeIto
Offline
Pages: 1
Topic closed
[ Started around 1732470187.0424 - Generated in 0.043 seconds, 12 queries executed - Memory usage: 1.46 MiB (Peak: 1.58 MiB) ]