tags:

views:

76

answers:

3

i want to call an exe file(abc.exe) from another application(hello)

how can i get the path of abc.exe file programatically so that i can use it to invoke abc.exe

abc.exe is not the part of executing assembly so assembly.GetExecutingAssembly() will not work

+1  A: 

Use system.diagnostics.process Class's capabilities to do this.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
public class MyProcess
{
    // These are the Win32 error code for file not found or access denied.
    const int ERROR_FILE_NOT_FOUND =2;
    const int ERROR_ACCESS_DENIED = 5;

    /// <summary>
    /// Prints a file with a .doc extension.
    /// </summary>
    public void PrintDoc()
    {
        Process myProcess = new Process();

        try
        {
            // Get the path that stores user documents.
            string myDocumentsPath = 
                Environment.GetFolderPath(Environment.SpecialFolder.Personal);

            myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
            myProcess.StartInfo.Verb = "Print";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
        }
        catch (Win32Exception e)
        {
            if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
                Console.WriteLine(e.Message + ". Check the path.");
            } 

            else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
                // Note that if your word processor might generate exceptions
                // such as this, which are handled first.
                Console.WriteLine(e.Message + 
                    ". You do not have permission to print this file.");
            }
        }
    }
Chathuranga Chandrasekara
but how to get the path of exe file
SWATI
Please clarify more? What do you mean by getting the path? Finding the absolute path when given just the file name?
Chathuranga Chandrasekara
+1  A: 

It sounds like you want to launch a separate process.

Process myProcess = new Process();
myProcess.StartInfo.FileName = @"c:\program files\hello\abc.exe"
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Isaac Cambron
@"c:\program files\hello\abc.exe"i cannot set path like this,it should be relative
SWATI
You can use Directory.GetCurrentDirectory() to get the current directory, and build a relative path from that.
Isaac Cambron
thanks 2 all, my work is done!!!!!!!!!!!!!!!!
SWATI
A: 

Thank u sooooooooooooo much for the help

SWATI