1

I am trying to run the DJOIN command from C#. (It is present by default in the c:\windows\system32 directory on Win 10.)

When I run the following:

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.FileName = @"c:\windows\system32\djoin.exe";
psi.RedirectStandardOutput = true;
psi.Arguments = "/C toast";
using (Process proc = Process.Start(psi))
{
using (StreamReader reader = proc.StandardOutput)
{
   string result = reader.ReadToEnd();
   MessageBox.Show(result);
}

I get a "file not found" error:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll    
Additional information: The system cannot find the file specified

enter image description here

However, if I use another "out of the box" .exe, such as "tasklist.exe" it works fine. E.g.:

proc.StartInfo.FileName = "tasklist.exe";

Gives me the following output:

enter image description here

Ben
  • 4,281
  • 8
  • 62
  • 103

3 Answers3

1

you can also disable to folder redirection for 64bit OS

[System.Runtime.InteropServices.DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);


        EnableWow64FSRedirection(false);

        try
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = false;
            psi.FileName = @"c:\windows\system32\djoin.exe";
            psi.RedirectStandardOutput = true;
            psi.Arguments = " /C toast ";
            using (Process proc = Process.Start(psi))
            {
                using (System.IO.StreamReader reader = proc.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    MessageBox.Show(result);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        EnableWow64FSRedirection(true);
Sorceri
  • 7,870
  • 1
  • 29
  • 38
0

Turns out the issue was that DJOIN.exe is 64-bit. My app was running in 32-bit, so I changed the platform to X64 and it worked.

See: Start process as 64 bit

Community
  • 1
  • 1
Ben
  • 4,281
  • 8
  • 62
  • 103
  • Any bitness can start a process of any bitness. Your app probably was 32 bit virtualized and the OS pretended the file did not exist. – usr May 05 '16 at 16:29
  • it was caused by folder redirection for the 64bit os – Sorceri May 05 '16 at 17:03
-1

Try to run the cmd command where your application is a parameter. The following works for me:

ProcessStartInfo processInfo = new ProcessStartInfo("cmd", @"djoin.exe /C toast");

processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;

Process ProcessObj = new Process();
ProcessObj.StartInfo = processInfo;

ProcessObj.Start();
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51