Kaya Suleyman
Kaya Suleyman

Reputation: 85

Directory.Exists() always returning false?

I am experiencing a strange problem at the moment with my C# code. I am attempting to use the folderBrowserDialog to retrieve the path of a folder selected by the user.

Once the user clicks a button to confirm the selection (the chosen path of which appears in "textBox1"), if the folder location is found, should return the message "connection established" (if the directory/file found) or "no connection found" (if the file/directory does not exist).

For some odd reason however, the code always seem to return false when checking if the directory exists - and yes, it does exist. My application requests admin rights in it's manifest file as I thought this would solve the problem, so I'm still stumped on this one.

private void button1_Click(object sender, EventArgs e)
{
  //BROWSE 
  folderBrowserDialog1.ShowDialog();
  textBox1.Text = folderBrowserDialog1.SelectedPath;
}

private void button2_Click(object sender, EventArgs e)
{
  var path = textBox1.Text + @"\" + "connection.pss";
  //ESTABLISH CONNECTION

  if (textBox1.TextLength > 0)
  {
    if (Directory.Exists(path))
    { 
      connectionstatus.Text = "CONNECTION ESTABLISHED!";
      //SET UP VARIABLES
    }

    if (!Directory.Exists(path))
    {
      connectionstatus.Text = "NO CONNECTION FOUND!";
    }
  }
}

Upvotes: 3

Views: 6165

Answers (2)

attila
attila

Reputation: 2219

Connection.pss is not part of the directory. Try either just checking the directory or use File.Exists()

Upvotes: 2

Michael Petrotta
Michael Petrotta

Reputation: 60902

That directory doesn't exist. That file exists. :)

Use File.Exists instead.

Upvotes: 19

Related Questions