PC Review


Reply
Thread Tools Rate Thread

Current Working Directory of a process

 
 
Blitr
Guest
Posts: n/a
 
      30th Mar 2006
Hi.

I'm writing an application in C# that runs a "cmd.exe" in another
process.

I need to get the working directory of the process without having to
resort to issueing "echo %CD%" to the cmd process.

Can anyone help me?

I'm going nuts trying to find a way to do this.

Cheers,
/Blitr

 
Reply With Quote
 
 
 
 
William Stacey [MVP]
Guest
Posts: n/a
 
      30th Mar 2006
I could be wrong here. A process has a notion of WorkingDirectory which can
be set, or if null, set to the parent process PWD. After that, the process
app may use that var or not. Shells like cmd.exe use that to set its *own
PWD internal var. CD gets and sets this internal var, but I am not sure
this also updates the ProcessInfo.WorkingDirectory var. So I think to get
the current dir of the shell, you would need to call some api on cmd.exe
itself (if you did not want to use echo %cd%). Maybe there is some com api
for it. The other issue (maybe) is when do you know the command that was
sent to the shell, via std input, is actually complete so you can update the
CWD in your UI? All your reading is an output stream of console output
(e.g. text), you don't really know when a script or exe is finished by
cmd.exe. cmd.exe knows, because it can wait for process exit and then
display the prompt again. But we just have an output stream that does not
tell you that. There must be some tricks as I think people have written UI
shell wrappers around cmd (I tried once and got stuck on the same issues).
Interested in a solution as well. Cheers and good luck.

--
William Stacey [MVP]

"Blitr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Hi.
|
| I'm writing an application in C# that runs a "cmd.exe" in another
| process.
|
| I need to get the working directory of the process without having to
| resort to issueing "echo %CD%" to the cmd process.
|
| Can anyone help me?
|
| I'm going nuts trying to find a way to do this.
|
| Cheers,
| /Blitr
|


 
Reply With Quote
 
Jack
Guest
Posts: n/a
 
      30th Mar 2006
I don't exactly know what u want todo and how it's done. I did have
some piece of code where I call an exe which returns text. Maybe it is
of any use to u. The Process.StartInfo has a WorkingDirectory property.

using System.IO;
using System.Diagnostics;

Process process = new Process();

process.StartInfo.FileName = m_pinLedgerReportExe;
process.StartInfo.Arguments = a_arguments;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

m_log.Debug("Start: " + m_pinLedgerReportExe + " " + a_arguments + " "
+ a_fileName);
string output = "";
process.Start();
while( !process.HasExited ) {
output += process.StandardOutput.ReadToEnd();
process.WaitForExit(50);
}
output += process.StandardOutput.ReadToEnd();

m_log.Info("GL's retrieved");

StreamWriter file = new StreamWriter(a_fileName);
file.AutoFlush = true;
file.Write(output);
file.Close();
process.Close();

m_log.Debug(a_fileName + " created");

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting Current/Working Directory for PowerPoint process through COM Automation chrismo Microsoft Powerpoint 10 17th Jul 2007 11:34 PM
Current Working Directory =?Utf-8?B?c3dhbWk=?= Microsoft Excel Programming 3 23rd Nov 2004 03:52 PM
Current Directory of another process? Viorel Microsoft Dot NET 0 21st Jul 2003 04:28 PM
Current Directory of another process? Viorel Microsoft Windows 2000 Developer 0 21st Jul 2003 04:28 PM
getting current directory of another process Viorel Microsoft Windows 2000 Developer 1 17th Jul 2003 07:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:51 AM.