Current Working Directory of a process

B

Blitr

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
 
W

William Stacey [MVP]

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]

| 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
|
 
J

Jack

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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top