簡體   English   中英

在其他位置處理另一個文件時,如何以編程方式獲取一個exe的路徑

[英]how can i get the path to one exe programatically while working with another file at other location

我想從另一個應用程序(您好)調用一個exe文件(abc.exe)

我如何以編程方式獲取abc.exe文件的路徑,以便可以使用它來調用abc.exe

abc.exe不是執行程序集的一部分,因此assembly.GetExecutingAssembly()無法正常工作

使用system.diagnostics.process類的功能來執行此操作。

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

聽起來您想啟動一個單獨的過程。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM