Simple File IO Question

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!
 
A

Arne Vajhøj

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.

File.Create("data.dat").Close()

should do it.
My question is, is this the proper way of doing things?

I guess it is OK even though I always have considered the
File utility methods to be way to VB'ish for my taste.

Arne
 
O

ozbear

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!

There are many ways of going about what you want to do, such as:

using (FileStream fs = new FileStream("data.dat",

FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None))
{
using (StreamReader sr = new StreamReader(fs))
{
// do stuff
}
}

The -using- statements ensure that both the underlying FileStream
and StreamReader objects get closed and Disposed of whether an
exception occurs or not. I prefer the FileStream constructor
rather than File.Create since it give you more options, which
you should look up to see what is most appropriate for you.

It seems a bit strange rhough that you would be wanting to employ
a StreamReader (notice -reader-) object against a file that has just
been created, since, by definition, it would be empty, there would
be no data to read, and StreamReader has no methds for writing to
the file.

If you really intended to create the file if it didn't exist and
-write- to it whether it already existed or not, the the problem
reduces to:

using (StreamWriter sr = new StreamWriter("data.dat"))
{
// do stuff
}
Oz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top