By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

File Ops

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Check out the File.Exists method:

http://msdn.microsoft.com/library/de...xiststopic.asp

- Jon
"hecsan07" <he******@hotmail.com> wrote in message
news:8E**********************************@microsof t.com...
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

Nov 16 '05 #2

P: n/a
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

"hecsan07" wrote:
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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.