Link to home
Start Free TrialLog in
Avatar of Raul77
Raul77

asked on

Execute a .EXE from Web Application

Hi Guys,
C# - IIS - Windows Server 2012
I am trying to execute a .exe file using a button on my Web Application, as a test first I have this code

            System.Diagnostics.Process process1 = new System.Diagnostics.Process();
            process1.StartInfo.FileName = @"notepad.exe";
            process1.Start();

Open in new window

in Task Manager, I can see Notepad is executed under "network Services"

Next i changed the above code to
            System.Diagnostics.Process process1 = new System.Diagnostics.Process();
            process1.StartInfo.FileName = @"MYOWN.exe";
            process1.Start();

Open in new window


no longer executes! MYOWN.EXE is in Windows/system32 folder

Can you please help.

Thank you,
Avatar of Misha
Misha
Flag of Russian Federation image

May be your process has already finished, when you look it?
Also try this code:
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "YourFileName.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        try
        {
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
            //your errors
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First of all:
MYOWN.EXE is in Windows/system32 folder

Guess why it's called Windows/System32.. it should not be in that path.

Then: What kind of web "application" is it? Cause the normal approach is using a Windows service for that, and let the web "application" talk to it.

And for your actual problem: Do you get an exception? When not, what does Start() return?

Finally: use the full qualified file name. Don't rely on PATH.
Avatar of Raul77
Raul77

ASKER

I tried the suggested code, didnt do the trick (it works fine with notepad.exe but not the .exe I want)
I have given full permission to file, from network services to Everyone
I have moved it to another folder and ensure permission is there
I have ensured the file is not executing
I have used the fully qualified file name
... I am not sure what else i can do here.
Oh i even tried impersonating it that part of the code as admin, didnt work!
did u try to put and link that .exe in another folder, rather  than System folder for testing purposes? will that worked for you?
Avatar of Raul77

ASKER

BTW, my application , literally has "1" button on the page, which links to the running a .exe file. thats all, its super simple.
Avatar of Raul77

ASKER

I put it in C:\Scripts\MYOWN.exe
and it did not work.
my application , literally has "1" button on the page, which links to the running a .exe file. thats all, its super simple.
is that button trying to launch an exe that resided in local machine or at server end?
Avatar of Raul77

ASKER

the .exe file is on the same server as IIS is in. so its local to IIS.
Avatar of Raul77

ASKER

    protected void Button2_Click(object sender, EventArgs e)
    {
       
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "notepad.exe";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            try
            {
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }
            }
            catch
            {
                //your errors
            }
        }

Open in new window

when i click on the button, I see Notepad in TaskManager under username "Network Service"
I replace notepad.exe with myown.exe ... and nothing happens !
so its local to IIS.
ok, meaning to say... you trying to launch an exe at server end?
Avatar of Raul77

ASKER

Correct. the .exe is local to the server.
Avatar of Raul77

ASKER

UPDATE: I changed the IIS POOL to run under an admin user instead of Network Services and it worked ! so i am assuming its a permission issue.
ok, so indeed it's permission issue as highlighted in my first comment, does it helped? glad that you resolved it, anyway.
as proposed.