当前位置:网站首页>C starts an external EXE file and passes in parameters
C starts an external EXE file and passes in parameters
2022-06-11 16:13:00 【Xiongsiyu】
In some of our commonly used software , Often some software , After double clicking, it cannot be opened at all , This is because there are restrictions on startup , We need to pass in some parameters to open , At work , This requirement can also be used for automatic software updates , stay Unity in , Yes XLua,ILruntime And so on , stay Winform in , because Windows The mechanism of , After opening the software , Can't do file replacement , We can use a software launcher , Update local software files , Then start the software , To achieve the purpose of automatic update , Of course , You can't double-click the software to open it .
Create a new one winform project , The interface is as follows , Used as a software starter

Just add a button , Add a click event to the button , The code is as follows :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace starter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "E:\\CSharp Project\\Test\\Test\\bin\\Debug\\Test.exe";
string[] parameter = {" Starter parameters 1", " Starter parameters 2", " Starter parameters 3", " Starter parameters 4" };
bool startResult = StartProcess(path, parameter);
if (startResult)
System.Environment.Exit(0);
}
/// <summary>
/// Start a software , And pass in parameters
/// </summary>
/// <param name="runFilePath"></param>
/// <param name="args"></param>
/// <returns></returns>
public bool StartProcess(string runFilePath, params string[] args)
{
string s = "";
foreach (string arg in args)
{
s = s + arg + " ";
}
s = s.Trim();
Process process = new Process();// Create process object
ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, s); // In brackets is ( The program name , Parameters )
process.StartInfo = startInfo;
process.Start();
return true;
}
}
}
Now create a second project , Used as the ontology of software
Create a new one winform project , There is no need to add anything in it , First set the output type of the software to console

Then open the startup script Program.cs , Add printing of parameters , Because when the software is just started , The console has just opened , You can't see the log if you print it immediately , So I add asynchronous delay operation here .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
static class Program
{
/// <summary>
/// The main entry point for the application .
/// </summary>
[STAThread]
static void Main(string[] args)
{
Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(5));
if (args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine(" Parameter is :" + args[i]);
}
}
else
Console.WriteLine("args Parameter is 0");
});
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
So let's test that out , Open the software launcher , Click button ,5 Seconds later , Then you can see the output of the console

Here you can set a limit on the parameters , If the parameters are incorrect , direct return, Users can't open the software directly
end
边栏推荐
- CLP information - financial standardization "14th five year plan" highlights data security
- 问题 AC: 中国象棋中的跳马问题
- Maui introductory tutorial series (1. framework introduction)
- laravel8 实现签到功能案例
- [sword finger offer] 22 The penultimate node in the linked list
- 搜索与图论:Dijkstra求最短路 I—Dijkstra(最短路径)
- How to manage concurrent write operations? Get you started quickly
- laravel 8 通过 任务调度 实现 数据库备份
- Laravel 8 realizes database backup through task scheduling
- 什么是泛型?为什么要使用泛型?泛型怎么用?那包装类呢?
猜你喜欢

laravel 监听模式

Opengauss version 3.0.0 was officially released, and immediately experience the first lightweight version in the community

Yef 2022 opened yesterday. The whole process of free live broadcast on multiple network platforms opened an online technology feast!

Database resource load management (Part 2)

【剑指Offer】21.调整数组顺序使奇数位于偶数前面

Maui introductory tutorial series (1. framework introduction)

整了20张高清数据分析全知识地图,强烈建议收藏!

Take you in-depth understanding of AGC cloud database

Cloud data management will break the island of storage and the island of team

Analysis of breadcrumb usage scenarios on websites
随机推荐
Thales cloud security report shows that cloud data leakage and complexity are on the rise
Laravel8 implementation of sign in function
Using cloud DB to build app quick start - quick application
Will you be punished for not wearing seat belts in the back row?
再聊数据中心网络
Opengauss version 3.0.0 was officially released, and immediately experience the first lightweight version in the community
After reading the book reading methods
Ai4db: AI slow SQL root cause analysis
基于ssm框架实现的企业进销存管理系统【源码+数据库+毕设】
真香,华为主动离职也给 N+1
It's really not human to let the express delivery arrive before the refund
Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description
List和Dict数据类型作用详解
Code farming essential SQL tuning (Part 1)
(OJ assignment of Hunan University of science and Technology) problem g: pattern matching of strings
How to predict SQL statement query time?
搜索与图论:Dijkstra求最短路 I—Dijkstra(最短路径)
Overview and operation of database dense equivalent query
Operation guide | how to select a collector on moonbeam and Moonriver
问题 AC: 中国象棋中的跳马问题