如何获取第二个添加项目的 exe 文件名?编辑

本文关键字:项目 exe 文件名 编辑 添加 第二个 何获取 获取 | 更新日期: 2023-09-27 18:35:51

在Form1中,我做了这个:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace test
{
    public partial class Form1 : Form
    {
        WindowsFormsApplication1.Form1 f1;
        public Form1()
        {
            InitializeComponent();

                    MessageBox.Show("Oops something went wrong sorry");
                    f1 = new WindowsFormsApplication1.Form1();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

其中 f1 是我刚刚添加的第二个项目。

现在我添加了 seocnd 项目作为参考。

在第二个项目中,我做了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string[] hardDrivedInfo;
        string applicationFileName; 
        public Form1()
        {
            InitializeComponent();
            applicationFileName = Path.GetDirectoryName(Application.ExecutablePath);

但是应用程序文件名向我显示了第一个项目的exe文件的路径,而我需要获取第二个项目的目录+文件名,该项目位于目录中:D:'C-Sharp'test'test'WindowsFormsApplication1'bin'Debug'WindowsFormsApplication1.exe

第一个项目的目录是:D:'C-Sharp'test'test'test'bin'Debug'test.exe

但是我需要使该应用程序文件名将显示:D:'C-Sharp'test'test'WindowsFormsApplication1'bin'Debug'WindowsFormsApplication1.exe

编辑**

我想做的是运行第一个主项目,然后在弹出消息框并关闭它后,它将运行第二个项目会将第二个项目 exe 文件复制到另一个位置,如 D:,并将运行第二个项目的 exe 文件。因此,如果我删除第一个项目 exe 文件,D: 上的第二个文件将继续运行。

如何获取第二个添加项目的 exe 文件名?编辑

你可以尝试使用

string file = typeof(Form1).Assembly.Location;

有关详细信息,请参阅Assembly.Location

包含清单的已加载文件的位置。如果加载的文件是卷影复制的,则位置是卷影复制后文件的位置。如果程序集是从字节数组加载的,例如在使用 Load(Byte[]) 方法重载时,则返回的值为空字符串 (")。

您的文本有点不清楚,但我猜您正在尝试获取引用的程序集的名称,而不是启动该过程的名称,对吗?

尝试使用 Assembly.GetExecutingAssembly() 。然后,可以从 Location 属性获取完整路径。