CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2001
    Posts
    104

    Determining if file exists

    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:

    Code:
    		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.

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured