重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
shell("cmd /c " 完整命令行和参数字符串,用两个分隔每行命令)
创新互联公司是一家专注于网站制作、成都做网站和内江服务器托管的网络公司,有着丰富的建站经验和案例。
可试试下面的方法:
1.可接收参数的外部程序
/// summary
/// 可接收参数的外部程序主函数
/// /summary
static class Program
{
/// summary
/// The main entry point for the application.
/// /summary
[STAThread]
static void Main(string[] paras)
{
string temp = "";
foreach (string str in paras)
{
temp += str + ",";
}
MessageBox.Show(temp);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
2.启动外部程序的方法(给外部程序加参数)
/// summary
/// 调用外部程序窗体
/// /summary
public partial class Invokeprogram : Form
{
public Invokeprogram()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = Application.StartupPath + "\\WindowsFormsApplication1.exe";
proc.StartInfo.Arguments = "-steam -game cstrike"; //传入启动参数
proc.Start();
//string output = proc.StandardOutput.ReadToEnd();
// MessageBox.Show(output);
}
}
VB.NET可选参数的默认值必须是一个常数表达式。
过程定义中跟在可选参数后的每个参数也都必须是可选的。
下面的语法显示带VB.NET可选参数的过程声明:
Sub sub name(ByVal parameter 1 As data type 1,
Optional ByVal parameter 2 As data type 2 = default value)
调用带VB.NET可选参数的过程
过程在运行时无法检测到给定的参数是否已被省略,或者调用代码是否已显式提供默认值。如果需要弄清楚这一点,可以设置一个不可能的值作为默认值。下面的过程定义了可选参数 office,并测试其默认值 QJZ 以查看它在调用中是否已被省略:
Visual Basic
Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
If office = "QJZ" Then
Debug.WriteLine("office not supplied -- using Headquarters")
office = "Headquarters" End If
' Insert code to notify headquarters or specified office.
End Sub
如果可选参数是像 String 这样的引用类型,只要它不是该变量所预期的值,就可以使用 Nothing 作为默认值。
VB.NET可选参数和重载
定义带可选参数的过程的另一种方法是使用重载。如果有一个可选参数,可以定义过程的两个重载版本,一个接受此参数,另一个则不带参数。此方法随可选参数数目的增加而变得更复杂。然而,这样做的优点是可以完全确定调用程序是否提供了每个VB.NET可选参数。
线程结束后利用委托生成事件返回,线程应用包括传入和传出参数。
Public Delegate Sub ThreadCallback(value As ThreadResult)
Public Class Form1
Private WithEvents _th_1 As Thread_1
Protected Overrides Sub OnLoad(e As System.EventArgs)
Dim value As ThreadObject
value.Index = 1
Me._th_1 = New Thread_1(Me)
Me._th_1.Run(value)
MyBase.OnLoad(e)
End Sub
Private Sub Thread_1_End(sender As Object, e As ThreadEventArgs) Handles _th_1.ThreadEnd
Me.TextBox1.Text = e.Result.Text
End Sub
End Class
Public Class Thread_1
Public Event ThreadEnd(sender As Object, e As ThreadEventArgs)
Private _control As Control
Sub New(control As Control)
Me._control = control
End Sub
Public Sub Run(value As Object)
Dim th As New Threading.Thread(AddressOf ThreadProc)
th.Start(value)
End Sub
Private Sub ThreadProc(obj As Object)
Dim value As ThreadObject = CType(obj, ThreadObject)
Dim result As ThreadResult = Nothing
If value.Index = 1 Then result.Text = "测试"
Dim callback As New ThreadCallback(AddressOf ThreadInvoke)
_control.Invoke(callback, result)
End Sub
Private Sub ThreadInvoke(value As ThreadResult)
RaiseEvent ThreadEnd(Me, New ThreadEventArgs(value))
End Sub
End Class
Public Structure ThreadObject
Public Index As Integer
'Public Rect As Rectangle
End Structure
Public Structure ThreadResult
Public Text As String
'Public Rect As Rectangle
End Structure
Public Class ThreadEventArgs
Inherits System.EventArgs
Private _result As ThreadResult
Public ReadOnly Property Result As ThreadResult
Get
Return _result
End Get
End Property
Sub New(value As ThreadResult)
Me._result = value
End Sub
End Class
比较专业的做法是在项目中添加微软的Application Settings类,详细帮助文档:
使用极其简单,假设在settings1.settings设置一个项目,名称:IP,类型:String,范围:用户,值:192.168.1.1
调用:Dim sIP As String = setting.IP
获取缺省值:Dim sIP As String = Settings1.Default.IP
保存: setting.IP = "192.168.1.30" : setting.Save()
Command 函数
给程序传递参数后,参数会保存在command函数中,通过处理command接受的参数字符串来来编写相应代码就行了
例如快捷方式为“D:\Test.exe -s”
程序写:
msgbox(command)
得到的结果为:“-s”