Solved

Cannot launch .exe in C#

Posted on 2014-01-03
20
890 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now