C#直接写参数,然后利用start()直接执行exe,exe部分传参怎么写
时间: 2023-11-27 17:51:45 浏览: 28
可以使用ProcessStartInfo类的Arguments属性来传递参数。示例代码如下:
```csharp
string exePath = @"C:\path\to\your\exe.exe";
string arguments = "argument1 argument2";
ProcessStartInfo startInfo = new ProcessStartInfo(exePath, arguments);
Process.Start(startInfo);
```
在上面的代码中,你需要将`exePath`替换为你自己的exe文件路径,`arguments`替换为你要传递的参数,多个参数可以用空格分隔。然后使用`ProcessStartInfo`类创建一个新的进程启动信息,将`exePath`和`arguments`传入构造函数。最后使用`Process.Start`方法启动新进程。
相关问题
C#直接写参数,然后利用start()直接执行
可以通过Process.Start()方法来启动一个外部程序,并且可以向该程序传递参数。下面是一个示例代码:
```
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
string path = "C:\\Program Files\\MyApp\\MyApp.exe";
string parameters = "/arg1 value1 /arg2 value2";
Process.Start(path, parameters);
}
}
```
在上面的示例中,我们使用Process.Start()方法启动了一个名为"MyApp.exe"的外部程序,并向该程序传递了两个参数:/arg1和/arg2,分别对应的值为value1和value2。
C# 写一个打包exe 的工具
以下是一个使用C#编写的简单打包exe工具,它可以将一个或多个文件打包成一个exe文件:
```csharp
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ExePacker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPackage_Click(object sender, EventArgs e)
{
// 选择需要打包的文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string[] fileNames = openFileDialog.FileNames;
// 选择exe文件保存路径
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Executable files (*.exe)|*.exe";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string outputPath = saveFileDialog.FileName;
// 读取exe文件模板
byte[] templateBytes = File.ReadAllBytes(Application.ExecutablePath);
int startIndex = (int)BitConverter.ToUInt32(templateBytes, 0x3c);
byte[] stubBytes = templateBytes.Skip(startIndex).ToArray();
// 读取需要打包的文件
byte[][] fileBytes = new byte[fileNames.Length][];
for (int i = 0; i < fileNames.Length; i++)
{
fileBytes[i] = File.ReadAllBytes(fileNames[i]);
}
// 合并exe文件模板和需要打包的文件
byte[] newBytes = new byte[stubBytes.Length + fileBytes.Sum(x => x.Length)];
stubBytes.CopyTo(newBytes, 0);
int offset = stubBytes.Length;
foreach (byte[] fileByte in fileBytes)
{
fileByte.CopyTo(newBytes, offset);
offset += fileByte.Length;
}
// 写入新的exe文件
File.WriteAllBytes(outputPath, newBytes);
// 设置exe文件属性
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.exe\OpenWithProgids", "ExePacker", new byte[0], RegistryValueKind.Binary);
Registry.SetValue(@"HKEY_CLASSES_ROOT\ExePacker\shell\open\command", null, "\"" + outputPath + "\" \"%1\"", RegistryValueKind.String);
MessageBox.Show("打包成功!");
}
}
}
}
}
```
这个工具使用了当前程序作为exe文件模板,读取模板文件的前部分作为stub,然后将需要打包的文件依次拼接到stub后面,生成新的exe文件。生成的exe文件会在当前目录下,文件名由用户指定。同时,它还会设置新的exe文件的属性,使得它可以作为程序打开其他exe文件的默认程序。