Solved

Cannot launch .exe in C#

Posted on 2014-01-03
20
908 Views
Last Modified: 2014-01-13
All, for some reason in C# WPF, this does  not launch the executable. Nothing happens when I click on the button.

 Any thoughts?

private void button1_Click_3(object sender, RoutedEventArgs e)
        {
           System.Diagnostics.Process.Start(@"C:\windows\System32\rstrui.exe");
        }

Open in new window

0
Comment
Question by:TeknikDev
  • 6
  • 3
  • 2
  • +5
20 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 39753601
Permissions?
TaskManager?
EventViewer?

There are number of tools to monitor what's happend...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39753625
With that path my first thought is permissions.
My second thought is does the file exist in that location?  (Check, don't assume)
0
 

Author Comment

by:TeknikDev
ID: 39753823
Hi Andy, yes the file is there and how do I fix the permission issue? I mean it is able to execute other files with other extensionsin that folder, but not any .exe files.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:TeknikDev
ID: 39763393
Anyone?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39770818
Are you sure the procedure is actually being hit? It may just be that your event has become detached from the control so isn't being called.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39770964
You should redirect the StandardOutput and StandardError to see if there are any errors.

Here is an example for redirecting StandardOutput:

C# RedirectStandardOutput
http://www.dotnetperls.com/redirectstandardoutput

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static void Main()
    {
	// Setup the process with the ProcessStartInfo class.
	var start = new ProcessStartInfo()
	{
              FileName = @"C:\7za.exe", // Specify exe name.
	      UseShellExecute = false,
	      RedirectStandardOutput = true,
        };
	
	// Start the process.
	using (var process = Process.Start(start))
	{
	    //
	    // Read in all the text from the process with the StreamReader.
	    //
	    using (var reader = process.StandardOutput)
	    {
		var result = reader.ReadToEnd();
		Console.Write(result);
	    }
	}
    }
}

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39770973
What happens if you run your C# WPF app with administrator rights ?  Do the button event start the exe without problems.  (If it does then it almost certainly a permissions issue).


ps.  I assume the exe does run if you double clicked it from the explorer - not that everything is actually working.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 39770977
rstrui.exe is usually located not in %windir%\system32 but in %windir%\system32\restore. There are several other files in that place, so even if you copied the exe to system32, maybe those files are missing. Try to launch it from c:\windows\system32\restore .
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39771432
Please post part of the code from XAML file where you have defined button1.

This is to make sure that you have attached "button1_Click_3" event correctly.

Thanks,
Ashok
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
ID: 39773649
Try this (you'll need to import System.Runtime.InteropServices):

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

Open in new window

private void button1_Click_3(object sender, RoutedEventArgs e)
        {
 IntPtr ptr = IntPtr.Zero;
            if (Wow64DisableWow64FsRedirection(ref ptr))
            {
                Process myProcess = new Process();
                myProcess.StartInfo.UseShellExecute = true;
                myProcess.StartInfo.FileName = "rstrui.exe";
                myProcess.Start();
                Wow64RevertWow64FsRedirection(ptr);
            }
        }
                                  

Open in new window


This is all because of 64 bit process can't access 32 bit path.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39776200
>>I mean it is able to execute other files with other extensionsin that folder, but not any .exe files.

That was from an earlier comment.  Odd the path is fine for anything other than an exe as the target.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 39776343
How do you execute files other than .exe??
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39776423
.pdf files will open with a pdf viewer.
.doc files will open with winword
.....

<appended>
Well I took it to mean the above, like you say almost anything else doesn't make sense (.bat files ?)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39776468
>> From the question: System.Diagnostics.Process.Start(@"C:\windows\System32\rstrui.exe");

This does work on my system
System.Diagnostics.Process.Start(@"C:\xyz\abc.doc");
will open the document abc.doc in winword (I have checked, this isn't an assumption)
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39776474
Hey all, All things can be run from Process.Start, it is same as opening any file from command prompt or run command in windows. The problem with system32 is it is in system native folder and can't be accessed by 64 bit process.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39776545
>>The problem with system32 is it is in system native folder and can't be accessed by 64 bit process.
The 'can't be accessed by 64 bit process' may well be correct BUT the asker does state earlier that there are no problems with the path for other types of file in that folder.  This puzzles me if the problem really is concerning a 64 bit process - all should fail to run/open in that case.

(Alternatively - giving incorrect information is a very good way not to get a problem solved, or at least not quickly.)
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39776565
For me I just tried the given code in my environment and It didn't worked for me either :( ,  then only I got into this solution with windows api.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is about my first experience with programming Arduino.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question