1
Hi Hind Nazim,
Add a reference to
System.Diagnostics and use the code below:
-----------------------------------------------------------------
OpenFileDialog ofd = new OpenFileDialog();
Process[] pname = Process.GetProcessesByName("wmplayer");
if (pname.Length == 0)
ofd.InitialDirectory = @"C:\DefaultPath";
else
ofd.InitialDirectory = @"C:\Users\username\My Music"; // I assume this exist on target system.
-------------------------------------------------------------------
Hope it helps!
Happy Coding :)
Accepted 0
0
Hi Sunny Kumar
I'm still struggling with this. Don't know how to do it.
0
Hi Sunny Kumar
Thanks for replying my question, but I'm trying to debug it gives the path of Microsoft Visual studio, not my running application path.
Not sure. How to set the initial directory to be read automatically with
BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string CurrentDirectory = Environment.CurrentDirectory;
Process[] pname = Process.GetProcessesByName("ProcessName");
if (pname.Length == 0)
ofd.InitialDirectory = @"C:\DefaultPath";
else
ofd.InitialDirectory = @"C:\Users\username\MyApplication\Project2"; // I want this to be read automatically instead of assigning the
To explain more my question,
I'm working on windows form application, and I have a browse button.. I have also an application (software) that creates project similler to the visual studio. this software usually needs to be running if I need to start my windows form application.
If I created a project and saved with a name "project2" and later I opened this project2 to read some files which are being saved under project2 folder, how to code the browse button to go directly to my software directory and open folder "project2" so that I can select some files from there. the task manager shows my running software under processes tab and under application tab you'll find the path of the folder project2. Can you help me on this, plz

0
Hi Hind Nizam,
I'm not pretty sure about what does it mean- "I'm working on a project which is running inside application" but I assume it that you want to get the Directory in which your current application is running, right?
If yes, the do it like this:
string BaseDirectory= AppDomain.CurrentDomain.BaseDirectory; // it will give you the directory where your exe file resides.
string currentDirectory=Environment.CurrentDirectory; it will give you the current directory under which your exe file is running.
Don't get confused with these both, they are quite different.
Suppose your application path is "C:\Users\username\MyApplication\myProject". It may be that the .exe file is being called from some other location (let it be C:) in command line or else, so in that case the currentDirectory would be C: but the Base directory would always be C:\Users\username\MyApplication\myProject.
I Hope I answered your question.
Happy Coding :)
0
Hi Sunny Kumar
Two more question, please.
Suppose I'm working on a project which is running inside application, and I need to open some files (related to this specific project), How to get the directory / path of that project.
previously, I asked if a program is running so that you checked the process and you set the initial directory (code below)
OpenFileDialog ofd = new OpenFileDialog();
Process[] pname = Process.GetProcessesByName("wmplayer");
if (pname.Length == 0)
ofd.InitialDirectory = @"C:\DefaultPath";
else
ofd.InitialDirectory = @"C:\Users\username\My Music";
So I need something like this:
@"C:\Users\username\MyApplication\myProject(project4) // but this will be detected automatically based on which project that I'm currently working on.
So, I need to check first if my program is running and then get the path of my project automatically so that when openFileDialog runs, it will go automatically to my project folder (which is one of multiple saved projects)
i need to do same also but with folderBrowserDialog

0
Cool ...
Thanks Sunny Kumar!