'get path for my current .exe ( Based only on its name )
string productName = AppDomain.CurrentDomain.BaseDirectory;
productName = "myapp.exe";
if (!File.Exists(productName)) {
MessageBox.Show("your message", "window title", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else {
//do nothing
}
I want this message box to show if I opened my application on whatever directory its in or current directory.
But then the logic of my app is that it copies itself to another directory with different name. so when i open it again i dont want this message box to appear. I know its a stupid logic but its a competition in my college I need to finish. also I tried System.Reflection.Assembly.GetEntryAssembly().Location; but everytime I open the app the message box appear and thats not what I wanted
Solution 1:[1]
You are assigning productName twice,
- With base directory
- With your
.exename.
Instead of assignment, you need to combine the base directory with .exe name to get full path of the "myapp.exe" file, using Path.Combine() method.
Like,
string productName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "myapp.exe");
if(!File.Exists(productName))
{
MessageBox.Show("your message", "window title", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//Your code goes here
}
MSDN: Path.Combine()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prasad Telkikar |
