Click to See Complete Forum and Search --> : Determining if file exists


dannytan
March 12th, 2003, 10:39 PM
hi all. i am currently writing a simple program. in my program, before the form loads, i would need to determine if a file named "abc.mdb" exists in the current directory of the running program. so below is what i did:


private void MainFrm_Load(object sender, System.EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
FileInfo fi = new FileInfo(curDir+"\\Abc.Mdb");
string fp = fi.FullName;

if(fi.Exists == false)
{
MessageBox.Show("System file Abc.Mdb is not found.", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
else
{
}
}



The problem is that EVEN the file EXISTS in the current directory fi.Exists is still false. Can anyone help? Thanks.

pareshgh
March 13th, 2003, 12:33 PM
what have happend is
string curDir = Directory.GetCurrentDirectory();

when you do this in debug mode then it will return the current directory as debug directory,
so what i generally do is :D
FileInfo fi = new FileInfo(curDir+"\\..\\..\\abc.mdb");
string fp = fi.FullName;
so it goes back in previous directory and then fetches.. or if you have particular path then you can type there.

note in my case Exists returns true..
if file is found in path and should also be true in ur case if actually file is found.

Paresh