0

I have to start an executable (installPrint.exe) within my C# code. For this purposes I used the System.Diagnostics.Process class. The exe file installs a printer driver and copy several files into different directories. I can execute the exe from command line and everything work fine. But if i execute the file with the Process class from my C# application, the printer driver will not be installed.

I start my C# application as a admin user on a Windows XP SP2 x86 machine. Why do my executable dont work in the context of my C# application? What possibilities do i have to get it work?

 ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.Arguments = "-i \"My Printer\" -dir . -port myPort -spooler";
        startInfo.CreateNoWindow = true;
        startInfo.FileName = @"C:\Printer\install.exe";
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        //startInfo.Verb = "runas";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.WorkingDirectory = @"C:\Printer\";
        session.Log("Working Directory: " + startInfo.WorkingDirectory);

        session.Log("Executing " + startInfo.FileName);
        try
        {
            Process process = new Process();
            //process.EnableRaisingEvents = false;
            process.StartInfo = startInfo;
            process.Start();

            session.Log("installer.exe started");
            StreamReader outReader = process.StandardOutput;
            StreamReader errReader = process.StandardError;
            process.WaitForExit();

            //session.Log(outReader.ReadToEnd());

            //session.Log(errReader.ReadToEnd());

            session.Log("RETURN CODE: " + process.ExitCode);

        }
        catch (Exception ex)
        {
            session.Log("An error occurred during printer installation.");
            session.Log(ex.ToString());
        }
CubaLibre
  • 375
  • 1
  • 2
  • 14
  • I get only the information that the printer driver couldn't be added. The installer also creates a local printer port. This works fine but when it comes to add the printer it fails. – CubaLibre Sep 29 '11 at 10:57
  • 1
    I found my failure. I setCreateNoWindow = false and used shell execute and now it works. – CubaLibre Sep 29 '11 at 12:53

2 Answers2

2

I take it, you are running your program on Windows Vista or 7. Then, you have to request elevation for your newly created process to run with full access rights. Look at those questions for details: Request Windows Vista UAC elevation if path is protected? Windows 7 and Vista UAC - Programmatically requesting elevation in C#

Ok, I see now, that you're using Win XP. Then it may be because of some settings of Process when you start it. Try to start you process as ShellExecute, this way it will be most close to normal starting by the user. Here's a sample:

var p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo { FileName = "yourfile.exe", UseShellExecute = true };
p.Start();
Community
  • 1
  • 1
Vladimir Perevalov
  • 4,059
  • 18
  • 22
  • I have tried withe useShellExecute and it has no effect. I dont understand whats the differnece between starting a process by user or by another process? Could it be because it is a child process of the executing process? – CubaLibre Sep 29 '11 at 10:33
  • Maybe the problem is in the driver package itself? Like it expects to be lanched from some particular place, etc. Maybe you should set working directory to the same directory where file is located? Because, AFAIK by default if you use Process.Start working directory will be the same as your host process. – Vladimir Perevalov Sep 29 '11 at 10:43
  • I am using the correct working directory. I added my sample code in the main post. – CubaLibre Sep 29 '11 at 10:49
  • For windows 7 your solution work, but now windows ask me for the administrator password two times. First when i start the installer, that is ok. Second during installation process (admin rights already given to installer) when the installer.msi starts for printer. Is there a way to avoid the second authorization? – CubaLibre Sep 29 '11 at 15:25
0

I use this class in many parts of my projects:

public class ExecutableLauncher
{
    private string _pathExe;

    public ExecutableLauncher(string pathExe)
    {
        _pathExe = pathExe;
    }
    public bool StartProcessAndWaitEnd(string argoment, bool useShellExecute)
    {
        try
        {
            Process currentProcess = new Process();

            currentProcess.EnableRaisingEvents = false;

            currentProcess.StartInfo.UseShellExecute = useShellExecute;

            currentProcess.StartInfo.FileName = _pathExe;

            // Es.: currentProcess.StartInfo.Arguments="http://www.microsoft.com";
            currentProcess.StartInfo.Arguments = argoment;

            currentProcess.Start();
            currentProcess.WaitForExit();
            currentProcess.Close();

            return true;
        }
        catch (Exception currentException)
        {
            throw currentException;
        }
    }
}

I hope to have answered at your question.

M.

mattpltt
  • 333
  • 1
  • 4
  • 18
  • 1
    I tried several options, with and without useShellExecute, and then i tried with startInfo.Verb = "runas". But nothing worked. I searched in the net for possibilities to get it work. It seems it is a problem accessing system settings while the process is executed from another process? – CubaLibre Sep 29 '11 at 11:03
  • Maybe there is a problem within the startInfo.Arguments = "-i \"My Printer\" -dir . -port myPort -spooler"; Try to check and remove some parameters. Are you sure of "." dir? – mattpltt Sep 29 '11 at 12:39
  • 1
    I have found the problem. see the comment in the main post. – CubaLibre Sep 29 '11 at 12:54