Hi
Using openFileDialog, would like to set a condition to check if a specific program is running or not. if it's running, then openFileDialog will set the initial directory to a specific folder for that program, otherwise it will go to another directory, else it will open the C: directory
Example:
If Windows media player is running then openFileDialog will open/set the directory of C>>Users>>username>>My Music folder
if Windows media player is not running, it will set/open My documents
else it will open the C: Directory
Sunny SharmaPosted Sep 6, 2013, 5:38 AM
Add a reference to System.Diagnostics and use the code below:
-----------------------------------------------------------------
Sunny SharmaPosted Sep 19, 2013, 6:01 AM
Sorry to make you wait a while, please go through this post:
http://www.c-sharpcorner.com/UploadFile/370e35/basedirectory-vs-currentdirectory-in-C-Sharp/
I hope it will help you understand this subject in a better way.
Share your code, what you've tried so far, input(s) and expected output if still have issues.
Happy Coding :)
Hind NajimPosted Sep 15, 2013, 1:25 PM
I'm still struggling with this. Don't know how to do it.
Hind NajimPosted Sep 10, 2013, 7:17 AM
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
Sunny SharmaPosted Sep 10, 2013, 12:46 AM
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 :)
Hind NajimPosted Sep 9, 2013, 2:57 PM
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
Hind NajimPosted Sep 6, 2013, 6:30 AM