Reputation: 115
I would like to check file exists in the same folder where is my program. If is do something. How i can fix that ?
private void button12_Click(object sender, EventArgs e)
{
if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "tres.xml"))
Upload("tres.xml");
}
Upvotes: 3
Views: 8223
Reputation: 460028
The reason why your code doesn't work is that GetDirectoryName
returns no \
at the end. That's even documented:
The string returned by this method consists of all characters in the path up to but not including the last
DirectorySeparatorChar
orAltDirectorySeparatorChar
Use Path.Combine
to get the correct directory separator char:
string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "tres.xml");
if(File.Exists(path))
{
// ...
}
Upvotes: 7
Reputation: 2950
You can just simply use:
File.Exists("tres.xml");
This checks the current directory of your .exe
Upvotes: 3