File Ops

G

Guest

I am a true new comer in C#, and I haven't done much with files. I am trying
to open a file for input, but I don't know how to check if the file exists
first. I have a pretty crappy book that's truly not helping. Can somebody
please help? I want to try to write to the file and if a file not found
exception is thrown, I wan to create the file. Please help.

HS
 
G

Guest

HS,
There is a static method:
System.IO.File.Exists()
Which will return true or false if the file exists.

If you're interested in opening up the file, you can check out the
"System.IO.FileInfo" class. Create this class, and it will give you a great
deal of information on the file you are interested in, as well as methods
Create() to get a file input stream, or CreateText() to get a text input
stream (if the file you are working with is just text).
The Visual Studio help (if you have the MSDN library installed) has some
great tutorial tracks on opening up different files, and reading and writing
data.

When working with files, you need to remember that your code needs access to
the directory location that the file resides. If you don't have access (like
if NTFS security has protected the folder in some way) a lot of the .NET
methods don't tell you this, just return false that the file doesnt exist.

Hope this helps:
Andrew Arace
 

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