C# 如何获取第二个添加项目的exe文件名?编辑
在表格1中,我这样做:C# 如何获取第二个添加项目的exe文件名?编辑,c#,C#,在表格1中,我这样做: 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; namespac
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);
但是applicationFileName向我显示第一个项目的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
编辑**
我想做的是运行第一个主项目,然后在messagebox弹出并关闭后,它将运行第二个项目,将第二个项目的exe文件复制到另一个位置,如D:并将运行第二个项目的exe文件。因此,如果我删除第一个project exe文件,D:上的第二个文件将继续运行。您的文本有点不清楚,但我猜您是在尝试获取引用程序集的名称,而不是启动进程的程序集的名称,对吗 尝试使用。然后您可以从
位置
属性获取完整路径。您可以尝试使用
string file = typeof(Form1).Assembly.Location;
有关更多信息,请参阅:
包含清单的已加载文件的位置。如果加载的文件是卷影复制的,则位置是卷影复制后的文件的位置。如果程序集是从字节数组加载的,例如使用Load(byte[])方法重载时,则返回的值为空字符串(“”)
为什么一个可执行文件引用另一个可执行文件?如果他们有需要共享的代码,那么应该将这些代码提取到类库中,并且两个应用程序都应该引用该库。相互引用的应用程序是不直观和混乱的(正如您所发现的)。@user…不清楚您想要什么。您是说希望…applicationFileName在两个项目中都显示:D:\C-Sharp\test\test\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe?MikeTWebb否我只想在第二个项目中获得第二个项目的可执行文件名以及完整路径。换句话说,我想将第二个项目的可执行文件名复制到另一个硬盘上,例如D:然后从D:运行此exe文件,即使我关闭或删除第一个项目的exe,第二个项目也会继续运行这就开始了这个过程。所以我尝试了你的想法,但没有找到任何属性:getExecutionGassembly()fro Assembly。我做到了:使用系统。反思;我参加了大会。然后我做了组装1。但是没有getExecutionGassembly(),可能问题是我在vs2010 pro和.net 4.0中使用的是一个静态方法,
getExecutionGassembly
是一个静态方法,因此可以在程序集上调用它。您不需要定义变量。尝试了Location属性,但效果不好。出于某种原因,向我显示了第一个项目的路径以及第二个项目末尾的路径。@user1363119:这就是程序集加载的来源-它作为构建过程的一部分被复制,但它显示了加载的文件。我看不出你怎么希望它知道文件最初是从哪里复制来的。