Scott McKeown

Hi All,

I have just started a new project and this has a XML file that is shared on a network drive.

I have put in my application a start up screen that asks where this file is located, now what I'm thinking is that if for some strange reason some one moves this XML file or the network share fails the application will not work.

So what I would like to do is, when the application loads it will first of all go off and look at this saved file location and see if the file is there, if not it will return a message either asking the user to check the file or enter the new location.

The problem I have is I don't even know where to start with this little idea of mine, so anyone with any pointers please could you help me out.

Thanks

Scott




Re: Visual C# Express Edition Is the file there?

IsshouFuuraibou

Best use is probably to create a FileInfo object and check to see if it exists. Alternatively, File has an exists static method. The same can be done with directories using DirectoryInfo/Directory.

FileInfo fi = new FileInfo( FileName );
if ( !fi.Exists )
// File does not exist

or

if ( !File.Exists(FileName) )
// File does not exist





Re: Visual C# Express Edition Is the file there?

jrboddie

Start with something like:

System.IO.FileInfo file = new FileInfo(yourFileName);

if(file.Exists == false)

{

MessageBox.Show("XML file is missing. Please check.");

}





Re: Visual C# Express Edition Is the file there?

Scott McKeown

Hi guys,

Thanks to both of you for the posts, I'll be looking them up later (when I have a PC with VS 2005 on that is), but just out of interest I have saved the file location into the application settings from the "Setup form" adding the file name on the end, no would I be correct in thinking that I could just place that straight into the (yourFileName) part of the code.

Sorry if I sound a bit dumb but rough day at work and C# is not really the top of my thinking plan at the moment.

Thanks again

Scott






Re: Visual C# Express Edition Is the file there?

Chris Dunaway

If you wish to check for a file without creating an instance of the FileInfo class, just use File.Exists(filename). IMO it reads better if you don't need the FileInfo object.


if (!File.Exists(filename)
{
MessageBox.Show("The file does not exist!");
}

Chris




Re: Visual C# Express Edition Is the file there?

Scott McKeown

Hi guys,

Thanks for the help I managed to get around to trying all your ideas and each one does just what I needed.

Why is it some of the simplest things are sometimes the hardest to work out for yourself

Nevermind though.

Thanks agian.

Scott