程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#進行Visio二次開發之Web端啟動繪圖客戶端並登錄(3)

C#進行Visio二次開發之Web端啟動繪圖客戶端並登錄(3)

編輯:關於C語言

3、Winform的繪圖客戶端程序能夠處理傳遞過來的命令行的參數,實現登錄啟動

首先介紹一個能夠處理命令行參數的公共類,他可以接受參數並把它放置到字典中

using System;
using System.Collections.Generic;
using System.Text;
namespace WHC.EDNMS.Commons
{
public class CommandArgs
{
Dictionary<string, string> mArgPairs = new Dictionary<string,string>();
public Dictionary<string, string> ArgPairs
{
get { return mArgPairs; }
}
public List<string> Params
{
get { return mParams; }
}
List<string> mParams = new List<string>();
}
public class CommandLine
{
/// <summary>
/// Parses the passed command line arguments and returns the result
/// in a CommandArgs object.
/// </summary>
/// The command line is assumed to be in the format:
///
/// CMD [param] [[-|--|\]&lt;arg&gt;[[=]&lt;value&gt;]] [param]
///
/// Basically, stand-alone parameters can appear anywhere on the command line.
/// Arguments are defined as key/value pairs. The argument key must begin
/// with a '-', '--', or '\'. Between the argument and the value must be at
/// least one space or a single '='. Extra spaces are ignored. Arguments MAY
/// be followed by a value or, if no value supplIEd, the string 'true' is used.
/// You must enclose argument values in quotes if they contain a space, otherwise
/// they will not parse correctly.
///
/// Example command lines are:
///
/// cmd first -o outfile.txt --compile second \errors=errors.txt third fourth --test = "the value" fifth
///
/// <param name="args">array of command line arguments</param>
/// <returns>CommandArgs object containing the parsed command line</returns>
public static CommandArgs Parse(string[] args)
{
char[] kEqual = new char[] { '=' };
char[] kArgStart = new char[] { '-', '\\' };
CommandArgs ca = new CommandArgs();
int ii = -1;
string token = NextToken( args, ref ii );
while ( token != null )
{
if (IsArg(token))
{
string arg = token.TrimStart(kArgStart).TrimEnd(kEqual);
string value = null;
if (arg.Contains("="))
{
string[] r = arg.Split(kEqual, 2);
if ( r.Length == 2 && r[1] != string.Empty)
{
arg = r[0];
value = r[1];
}
}
while ( value == null )
{
string next = NextToken(args, ref ii);
if (next != null)
{
if (IsArg(next))
{
ii--;
value = "true";
}
else if (next != "=")
{
value = next.TrimStart(kEqual);
}
}
}
ca.ArgPairs.Add(arg, value);
}
else if (token != string.Empty)
{
ca.Params.Add(token);
}
token = NextToken(args, ref ii);
}
return ca;
}
static bool IsArg(string arg)
{
return (arg.StartsWith("-") || arg.StartsWith("\\"));
}
static string NextToken(string[] args, ref int ii)
{
ii++;
while ( ii < args.Length )
{
string cur = args[ii].Trim();
if (cur != string.Empty)
{
return cur;
}
ii++;
}
return null;
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved