shell("cmd /c " 完整命令行和參數(shù)字符串,用兩個分隔每行命令)
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站制作、成都做網(wǎng)站和內江服務器托管的網(wǎng)絡公司,有著豐富的建站經(jīng)驗和案例。
可試試下面的方法:
1.可接收參數(shù)的外部程序
/// summary
/// 可接收參數(shù)的外部程序主函數(shù)
/// /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.啟動外部程序的方法(給外部程序加參數(shù))
/// 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"; //傳入啟動參數(shù)
proc.Start();
//string output = proc.StandardOutput.ReadToEnd();
// MessageBox.Show(output);
}
}
VB.NET可選參數(shù)的默認值必須是一個常數(shù)表達式。
過程定義中跟在可選參數(shù)后的每個參數(shù)也都必須是可選的。
下面的語法顯示帶VB.NET可選參數(shù)的過程聲明:
Sub sub name(ByVal parameter 1 As data type 1,
Optional ByVal parameter 2 As data type 2 = default value)
調用帶VB.NET可選參數(shù)的過程
過程在運行時無法檢測到給定的參數(shù)是否已被省略,或者調用代碼是否已顯式提供默認值。如果需要弄清楚這一點,可以設置一個不可能的值作為默認值。下面的過程定義了可選參數(shù) 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
如果可選參數(shù)是像 String 這樣的引用類型,只要它不是該變量所預期的值,就可以使用 Nothing 作為默認值。
VB.NET可選參數(shù)和重載
定義帶可選參數(shù)的過程的另一種方法是使用重載。如果有一個可選參數(shù),可以定義過程的兩個重載版本,一個接受此參數(shù),另一個則不帶參數(shù)。此方法隨可選參數(shù)數(shù)目的增加而變得更復雜。然而,這樣做的優(yōu)點是可以完全確定調用程序是否提供了每個VB.NET可選參數(shù)。
線程結束后利用委托生成事件返回,線程應用包括傳入和傳出參數(shù)。
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
比較專業(yè)的做法是在項目中添加微軟的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 函數(shù)
給程序傳遞參數(shù)后,參數(shù)會保存在command函數(shù)中,通過處理command接受的參數(shù)字符串來來編寫相應代碼就行了
例如快捷方式為“D:\Test.exe -s”
程序寫:
msgbox(command)
得到的結果為:“-s”