C
CSharp-Jay
Just a question, I have noticed in my applications that when you try
to read a file that doesn't exist, this bombs your application. Of
course I could just write an error handler for the issue but instead
in alot of my applications if it doesn't exist I just want to create
it. So I usually end up with code like this:
if(!File.Exists("data.dat"))
File.Create("data.dat")
StreamReader sr = new StreamReader("data.dat");
// insert relevant code here
sr.Close();
The issue is that when the program gets to the StreamReader
instantiation it then bombs out with a file is in use error. I know
why it is bombing out, I guess I just don't know how to get the first
2 lines of code to close the file so I can move on with the rest of
it. My question is, is this the proper way of doing things? If so,
what is missing? If not, how should I have done it? Any help is
appreciated, thank you!
to read a file that doesn't exist, this bombs your application. Of
course I could just write an error handler for the issue but instead
in alot of my applications if it doesn't exist I just want to create
it. So I usually end up with code like this:
if(!File.Exists("data.dat"))
File.Create("data.dat")
StreamReader sr = new StreamReader("data.dat");
// insert relevant code here
sr.Close();
The issue is that when the program gets to the StreamReader
instantiation it then bombs out with a file is in use error. I know
why it is bombing out, I guess I just don't know how to get the first
2 lines of code to close the file so I can move on with the rest of
it. My question is, is this the proper way of doing things? If so,
what is missing? If not, how should I have done it? Any help is
appreciated, thank you!