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

时间:2012-05-29 18:20:23

标签: c#

在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);

但是applicatioFileName向我展示了第一个项目的exe文件的路径,而我需要获取目录中第二个项目的目录+文件名:D:\C-Sharp\test\test\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

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

但我需要让applicationFileName显示:D:\C-Sharp\test\test\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

编辑**

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

2 个答案:

答案 0 :(得分:1)

您可以尝试使用

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

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

  

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

答案 1 :(得分:0)

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

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

相关问题