如何在处理其他位置的另一个文件时以编程方式获取一个exe的路径

时间:2010-01-19 04:41:27

标签: c# .net

我想从另一个应用程序(你好)调用exe文件(abc.exe)

如何以编程方式获取abc.exe文件的路径,以便我可以使用它来调用abc.exe

abc.exe不是执行程序集的一部分 所以 assembly.GetExecutingAssembly()将无法正常工作

2 个答案:

答案 0 :(得分:1)

使用system.diagnostics.process Class的功能来执行此操作。

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.");
            }
        }
    }

答案 1 :(得分:1)

听起来你想要启动一个单独的过程。

Process myProcess = new Process();
myProcess.StartInfo.FileName = @"c:\program files\hello\abc.exe"
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();