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

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

編輯:關於C#

有這樣的需求,一個系統,包含Web端的後台和Winform的繪圖客戶端程序,用戶需要在Web端能夠啟動繪圖客戶端,並且不需要重新登錄(因為已經登錄了Web端了)。

那麼在IE的Web端,如何啟動Winform做的繪圖客戶端程序呢?當然對於其他桌面應用程序也是一樣的。

總體思路是:

1. 在asp.net頁面中增加一個按鈕或者菜單,連接是調用一個JavaScript函數實現啟動程序

2. 客戶端的用戶的環境變量有該應用程序的目錄路徑信息

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

詳細操作介紹如下:

1、asp.net頁面中Javascript的代碼如下:

javascript:Run('EDNMS.UI.exe -u admin -p 4f5a51484e3c639b7c0e606511fe062d5f55aa0509638b385ed179e6d7fe4e9b7342f04c7c74b625574d6aa009693f386cef7b49536c3a4bfb5372675e76bb134f746a84466b7da86703');

<script type="text/javascript" language="JavaScript">
function Run(command)
{
window.oldOnError = window.onerror;
window._command = command;
window.onerror = function (err){
if(err.indexOf('automation') != -1){
alert('命令已經被用戶禁止!');
return true;
}
else return false;
};
try
{
var wsh = new ActiveXObject('WScript.Shell');
if(wsh)
wsh.Run(command);
window.onerror = window.oldOnError;
}
catch (e)
{
alert('找不到文件EDNMS-DE.EXE(或它的組件之一)。請確定路徑和文件名是否正確,而且所需的庫文件均可用。')
}
}
</script>

2、為了使得Web端的Javascript能夠調用EDNMS.UI.exe 的Winform程序,我們需要在安裝Winform的時候,把安裝路徑加入到操作系統Path變量中,操作系統的Path變量的內容放置在注冊表節點SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment的Path中,下面是自定義安裝操作的代碼。

[RunInstaller(true)]
public class InstallAction : Installer
{
private string virtualRoot = string.Empty; // 安裝虛擬路徑
private string physicalRoot = string.Empty; // 安裝物理路徑
/// <summary>
/// 必需的設計器變量。
/// </summary>
private Container components = null;
public InstallAction()
{
// 該調用是設計器所必需的。
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
try
{
......
//修改Path環境變量
UpdatePathEnvironment();
}
catch (Exception ex)
{
WriteLog(ex.Message + "\r\n " + ex.StackTrace);
}
}
/// <summary>
/// 加入安裝文件的路徑,方便Web端訪問
/// </summary>
private void UpdatePathEnvironment()
{
//得到原來Path的變量值
string registerKey = "SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment";
string key = "Path";
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(registerKey);
string result = regKey.GetValue(key).ToString();
//添加新的值
if (result.IndexOf(physicalRoot) < 0)
{
result += string.Format(";{0}", physicalRoot);
}
regKey = Registry.LocalMachine.OpenSubKey(registerKey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);
regKey.SetValue(key, result);
}
......
}

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;
}
}
}

然後在程序的入口Main函數中,增加對參數化的登錄解析,如下所示

[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
//args = new string[] { "-u ", "admin", "-p", "4e0a40090737639a661f6e7109f1062c585dff410f668c3c5f836caf8ef54e9a695bfe48647bb62450457fe40b6c383c6dbd6e0002673a4ae14a74634679bb12487c7fc0406e7aac6611" };
LoginByArgs(args);
}
else
{
LoginNormal(args);
}
}
/// <summary>
/// 使用參數化登錄
/// </summary>
/// <param name="args"></param>
private static void LoginByArgs(string[] args)
{
CommandArgs commandArgs = CommandLine.Parse(args);
if (commandArgs.ArgPairs.Count > 0)
{
#region 獲取用戶參數
string userName = string.Empty;
string identity = string.Empty;
foreach (KeyValuePair<string, string> pair in commandArgs.ArgPairs)
{
if ("U".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
{
userName = pair.Value;
}
if ("P".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
{
identity = pair.Value;
}
}
#endregion
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(identity))
{
bool bLogin = Portal.gc.LoginByIdentity(userName.Trim(), identity);
if (bLogin)
{
ShowMainDialog();
}
else
{
LoginNormal(args);
}
}
}
}

至此,當客戶端安裝了繪圖客戶端後,Path的路徑將加入安裝的路徑,在Web端通過javascript調用程序啟動即能進行響應,並通過CommandLine輔助類解析參數後進行登錄。

但要主要的是,Javascript能夠調用本地的程序,是需要在IE中設置啟用Javascript權限許可才可以。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved