C# Form Application Process.Start() Gives Access is denied Error

1

I have an application which gives a cross thread exception when I try to run it with its commands.

    /// <summary>
    /// to run the EXE file in the c# code
    /// </summary>
    /// <param name="exeFile">the exe file path and name</param>
    /// <param name="commandLine"></param>
    /// <param name="logArea"></param>
    /// <param name="startButton"></param>
    private void RunExe(string exeFile, string commandLine, RichTextBox logArea, Button startButton)
    {
        process = new Process
        {
            StartInfo =
            {
                FileName = exeFile,
                Arguments = commandLine,
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            }
        };
        //process instant
        //shell name
        //adding command line as argument to the shell
        //listen to the outputs - non error stream
        //listen to the outputs - error stream
        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                logArea.Invoke(new Action(() =>
                {
                    AppendLine(e.Data, logArea);
                    Application.DoEvents();
                }));
            }
        });

        process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            logArea.Invoke(new Action(() =>
            {
                AppendLine(e.Data, logArea);
                Application.DoEvents();
            }));
        });

        Cursor.Current = Cursors.WaitCursor;
        startButton.Invoke(new Action(() => startButton.Enabled = false));

        try
        {
            process.Start(); //start the process instant
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
        }
        finally
        {
            Cursor.Current = Cursors.Default;
            startButton.Invoke(new Action(() => startButton.Enabled = true));
            GenerateButtonEnable(true);
        }

        process.Close();

        if (startButton == buttonGenerateUT)
        {
            if (UTMatrixStopButton.InvokeRequired)
            {
                UTMatrixStopButton.Invoke(new MethodInvoker(delegate
                {
                    UTMatrixStopButton.Enabled = false;
                }));
            }
            else
            {
                UTMatrixStopButton.Enabled = false;
            }
        }
        else
        {   // invokes required when cross thread tries to reach the ui
            if (IRStopButton.InvokeRequired)
            {
                IRStopButton.Invoke(new MethodInvoker(delegate
                {
                    IRStopButton.Enabled = false;
                }));
            }
            else
            {
                IRStopButton.Enabled = false;
            }
        }
    }

This is my function when I start the process Process.Start gives the following error. Funny thing is, everything works fine when I use it as an application. When I try to debug it in the VS, it gives this error.

enter image description here

Full error: System.ComponentModel.Win32Exception HResult=0x80004005 Message=Access is denied Source=System StackTrace: at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at SoftwareTestMatrixTool.MainForm.RunExe(String exeFile, String commandLine, RichTextBox logArea, Button startButton) in C:\MYPATH\MainForm.cs:line 669 at SoftwareTestMatrixTool.MainForm.<>c__DisplayClass15_0.b__0() in C:\MYPATH\MainForm.cs:line 613 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

The error shows the caller line of this function.

c#
winforms
asked on Stack Overflow Jun 14, 2019 by Alper Ayna • edited Jun 14, 2019 by Panagiotis Kanavos

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0