Solved

Cannot launch .exe in C#

Posted on 2014-01-03
20
920 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

839 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