C# 使用 Environment.GetCommandLineArgs 方法 製作*.exe和參數 供其他程式調用

來源:互聯網
上載者:User

Environment..::.GetCommandLineArgs 方法 :

 返回包含當前進程的命令列參數的字串數組。

文法:

 public static string[] GetCommandLineArgs()

傳回值
類型:System.String[]
字串數組,其中的每個元素都包含一個命令列參數。第一個元素是可執行檔名,後面的零個或多個元素包含其餘的命令列參數。

備忘:

數組中的第一個元素包含執行程式的檔案名稱。如果該檔案名稱不可用,則第一個元素等於 String..::.Empty。其他元素包含在命令列輸入的任何附加標記。

程式檔案名稱可以(但不是必須)包含路徑資訊。

命令列參數由空格分隔。可以使用雙引號 (") 在參數中包含空格。但是,單引號 (') 不提供此功能。

如果兩個或偶數個反斜線後跟雙引號,則前面的每個反斜線對被一個反斜線替代,並且雙引號被刪除。如果奇數個(包括僅僅一個)反斜線後跟雙引號,則前面的每個反斜線對被一個反斜線替代,其餘的反斜線被刪除;但在此情況下,雙引號不會被刪除。
下表顯示如何分隔命令列參數,並假定 MyApp 為當前執行的應用程式。

命令列上的輸入內容

產生的命令列參數

MyApp alpha beta

MyApp, alpha, beta

MyApp "alpha with spaces" "beta with spaces"

MyApp, alpha with spaces, beta with spaces

MyApp 'alpha with spaces' beta

MyApp, 'alpha, with, spaces', beta

MyApp \\\alpha \\\\"beta

MyApp, \\\alpha, \\bet

MyApp \\\\\"alpha \"beta

MyApp, \\"alpha, "beta

若要擷取作為單個字串的命令列,請使用 CommandLine 屬性。

Example代碼:

using System;
……
using System.Windows.Forms;

namespace ReadDocument
{
    public partial class FrmReadDocument : Form
    {
        public FrmReadDocument()
        {
            InitializeComponent();
        }

        private void FrmReadDocument_Load(object sender, EventArgs e)
        {
            this.Location = new Point(0,0);

            //string[] cmdArgs = Environment.GetCommandLineArgs();
            //string strCmdArgs = "";
            //if (cmdArgs.Count() > 0)
            //{
            //    foreach (string str in cmdArgs)
            //    {
            //        strCmdArgs = strCmdArgs + str + "\n";
            //    }
            //}
            //MessageBox.Show(strCmdArgs);

            //Process process = Process.Start(str, "\"" + FileName + "\"");

            try
            {
                string[] arg = Environment.GetCommandLineArgs();
                string argstrs = String.Join(",", arg);
                //MessageBox.Show("argstrs: "+argstrs);
                string[] argPath = argstrs.Split(",".ToCharArray());
                string filePath = argPath[1].ToString();
                string strarr = "*.exe: " + argPath[0].ToString() + "\r\n" + "args: " + argPath[1].ToString();
                //MessageBox.Show(strarr);
                this.webBrowser1.Navigate(filePath, false);
                this.Activate();
            }
            catch (Exception ex)
            {
                Logs.WriteLog(ex.Message.ToString());
            }
        }

        private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
                this.Dispose();
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.webBrowser1.Visible = true;
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            this.BackgroundImage = ReadDocument.Properties.Resources.load;
            webBrowser1.Visible = false;
        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            this.webBrowser1.Navigate(((System.Windows.Forms.WebBrowser)sender).StatusText, false);
        }

        private void FrmReadDocument_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.