802 lines
32 KiB
C#
802 lines
32 KiB
C#
![]() |
using System;
|
|||
|
using System.Collections;
|
|||
|
using static System.Configuration.ConfigurationSettings;
|
|||
|
using System.Data;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
using Microsoft.VisualBasic;
|
|||
|
using Microsoft.VisualBasic.CompilerServices;
|
|||
|
using static iMESCore.Base.iMESConst;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Resources;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace AutoLoaderLib_Base
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
static class modWIN
|
|||
|
{
|
|||
|
|
|||
|
// ReviseDate : 2003/08/07,修改處理AdditionalXML
|
|||
|
// ReviseDate : 2003/08/05,新增Public gReturnArray
|
|||
|
// ReviseDate : 2003/07/01,更改AddFlow產生的Error(CInput)
|
|||
|
// ReviseDate : 2002/11/04,新增CUnInput function
|
|||
|
|
|||
|
public static string gComputerName;
|
|||
|
public static string gReturnKeyValue;
|
|||
|
public static string gUserNo;
|
|||
|
public static string gUserName;
|
|||
|
public static string gUserLevel;
|
|||
|
public static string gLanguageMode;
|
|||
|
public static string gAppPath;
|
|||
|
// **Add for Smart Client Architecture
|
|||
|
public static string gMESWebServiceHost;
|
|||
|
public static string gCUSWebServiceHost;
|
|||
|
public static bool gEnableSSL = Convert.ToBoolean(GetAppSettings("EnableSSL"));
|
|||
|
|
|||
|
// Add By Peter 2005/6/20
|
|||
|
public static string gSettingMode = "";
|
|||
|
|
|||
|
public static DataRow drSearch;
|
|||
|
|
|||
|
public static ArrayList gReturnArray = new ArrayList();
|
|||
|
|
|||
|
public const string strAddTagLabel = "extrabase"; // Add by py for Combine Addition XML doc
|
|||
|
public const string strAddTagName = "ExtraBase"; // Add by py for Combine Addition XML doc
|
|||
|
public static string gSTDPath = GetAppSettings("STDPath");
|
|||
|
public static bool chkExecutionSuccess(ref System.Xml.XmlDocument Xmldoc)
|
|||
|
{
|
|||
|
bool chkExecutionSuccessRet = default(bool);
|
|||
|
if (Xmldoc.DocumentElement["result"].InnerXml == "success")
|
|||
|
{
|
|||
|
chkExecutionSuccessRet = true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
chkExecutionSuccessRet = false;
|
|||
|
}
|
|||
|
|
|||
|
return chkExecutionSuccessRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string GetExceptionSysMsg(ref System.Xml.XmlDocument Xmldoc)
|
|||
|
{
|
|||
|
string GetExceptionSysMsgRet = default(string);
|
|||
|
string argstrInput = Xmldoc.DocumentElement.GetElementsByTagName("sysmsg").Item(0).InnerXml;
|
|||
|
GetExceptionSysMsgRet = CUnInput(ref argstrInput);
|
|||
|
Xmldoc.DocumentElement.GetElementsByTagName("sysmsg").Item(0).InnerXml = argstrInput;
|
|||
|
return GetExceptionSysMsgRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string GetExceptionMesMsg(ref System.Xml.XmlDocument Xmldoc)
|
|||
|
{
|
|||
|
string GetExceptionMesMsgRet = default(string);
|
|||
|
string argstrInput = Xmldoc.DocumentElement.GetElementsByTagName("mesmsg").Item(0).InnerXml;
|
|||
|
GetExceptionMesMsgRet = CUnInput(ref argstrInput);
|
|||
|
Xmldoc.DocumentElement.GetElementsByTagName("mesmsg").Item(0).InnerXml = argstrInput;
|
|||
|
return GetExceptionMesMsgRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineXMLIdentity(ref string ComputerName, ref string CurUserNo, ref string SendTime)
|
|||
|
{
|
|||
|
string CombineXMLIdentityRet = default(string);
|
|||
|
CombineXMLIdentityRet = "<computername>" + ComputerName + "</computername>" + "<curuserno>" + CurUserNo + "</curuserno>" + "<sendtime>" + SendTime + "</sendtime>";
|
|||
|
|
|||
|
return CombineXMLIdentityRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineXMLParameter(ref string value_name, ref string name, ref string type, ref string value, ref string desc)
|
|||
|
{
|
|||
|
string CombineXMLParameterRet = default(string);
|
|||
|
CombineXMLParameterRet = "<" + value_name.ToLower() + ">" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "<desc>" + desc + "</desc>" + "</" + value_name.ToLower() + ">";
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return CombineXMLParameterRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineXMLRequest(ref string strIdentity, ref string strParameter)
|
|||
|
{
|
|||
|
string CombineXMLRequestRet = default(string);
|
|||
|
CombineXMLRequestRet = "<request>" + "<identity>" + strIdentity + "</identity>";
|
|||
|
if (!string.IsNullOrEmpty(strParameter))
|
|||
|
{
|
|||
|
CombineXMLRequestRet = CombineXMLRequestRet + "<parameter>" + strParameter + "</parameter>";
|
|||
|
}
|
|||
|
CombineXMLRequestRet = CombineXMLRequestRet + "</request>";
|
|||
|
return CombineXMLRequestRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineXMLValue(ref string TagName, ref string Value)
|
|||
|
{
|
|||
|
string CombineXMLValueRet = default(string);
|
|||
|
CombineXMLValueRet = "<" + TagName + ">" + Value + "</" + TagName + ">";
|
|||
|
return CombineXMLValueRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineXMLValueTag(ref string Value)
|
|||
|
{
|
|||
|
string CombineXMLValueTagRet = default(string);
|
|||
|
CombineXMLValueTagRet = "<value>" + Value + "</value>";
|
|||
|
return CombineXMLValueTagRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineXMLParameterMultiValue(ref string value_name, ref string name, ref string type, ref string value, ref string desc)
|
|||
|
{
|
|||
|
string CombineXMLParameterMultiValueRet = default(string);
|
|||
|
// Value不用加上Tag
|
|||
|
CombineXMLParameterMultiValueRet = "<" + value_name.ToLower() + ">" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + value + "<desc>" + desc + "</desc>" + "</" + value_name.ToLower() + ">";
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return CombineXMLParameterMultiValueRet;
|
|||
|
}
|
|||
|
|
|||
|
public static int FindRecordPosition(ref DataView dvData, ref string strColumnName, ref string strFindValue)
|
|||
|
{
|
|||
|
int FindRecordPositionRet = default(int);
|
|||
|
// //由Dataview的第一筆開始尋找符合的資料直到最後一筆
|
|||
|
// //傳出資料在資料表內的Index
|
|||
|
int i;
|
|||
|
bool Found; // //紀錄是否找到符合資料
|
|||
|
string strDataType; // //尋找資料欄的資料型態
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(strFindValue)) // 是否有傳入尋找的條件
|
|||
|
{
|
|||
|
Found = false;
|
|||
|
strDataType = dvData.Table.Columns[strColumnName].DataType.ToString();
|
|||
|
if (strDataType == "System.DateTime") // 日期型態的資料比對
|
|||
|
{
|
|||
|
var loopTo = dvData.Count;
|
|||
|
for (i = 0; i <= loopTo; i++)
|
|||
|
{
|
|||
|
if (Conversions.ToBoolean(Operators.ConditionalCompareObjectGreaterEqual(dvData[i][strColumnName], "#" + Strings.Format(Conversions.ToDate(strFindValue), "yyyy/MM/dd") + "#", false)))
|
|||
|
{
|
|||
|
Found = true;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else // 字串,數值資料型態的比對
|
|||
|
{
|
|||
|
var loopTo1 = dvData.Count;
|
|||
|
for (i = 0; i <= loopTo1; i++)
|
|||
|
{
|
|||
|
if ((Strings.UCase(dvData[i][strColumnName].ToString()) ?? "") == (Strings.UCase(strFindValue) ?? ""))
|
|||
|
{
|
|||
|
Found = true;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if (Found == true)
|
|||
|
{
|
|||
|
FindRecordPositionRet = i; // 找到符合資料,傳出所在的RowIndex
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
FindRecordPositionRet = -1;
|
|||
|
} // 沒有符合資料
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
FindRecordPositionRet = -1;
|
|||
|
} // 沒有符合資料
|
|||
|
|
|||
|
return FindRecordPositionRet;
|
|||
|
|
|||
|
// //使用Dataview的Find Method,日期無法使用
|
|||
|
// dvData.Sort = strColumnName
|
|||
|
// FindRecordPosition = dvData.Find(strFindValue)
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static string PasswordEncoding(ref string Password)
|
|||
|
{
|
|||
|
string PasswordEncodingRet = default(string);
|
|||
|
// 此 Function 將傳入值加以編碼後傳出,編碼後長度不變
|
|||
|
// 傳入值: 密碼
|
|||
|
// 傳回值: 編碼後密碼
|
|||
|
|
|||
|
// Vernam密碼是由Gilbert Vernam在1918年發明的
|
|||
|
string g_Key = "xNDFz6LH67LOv7xKbWFpbMu1wejrM7SzvV4tLRvq3X47m708O1xMHLoaMNCqGhoaEN";
|
|||
|
string strChar, iCryptChar, strEncrypted = default(string);
|
|||
|
int i, iKeyChar, iStringChar;
|
|||
|
|
|||
|
var loopTo = Strings.Len(Password);
|
|||
|
for (i = 1; i <= loopTo; i++)
|
|||
|
{
|
|||
|
iKeyChar = Strings.Asc(Strings.Mid(g_Key, i, 1));
|
|||
|
iStringChar = Strings.Asc(Strings.Mid(Password, i, 1));
|
|||
|
iCryptChar = (iKeyChar ^ iStringChar).ToString();
|
|||
|
strEncrypted = strEncrypted + Strings.Chr(Conversions.ToInteger(iCryptChar));
|
|||
|
}
|
|||
|
|
|||
|
PasswordEncodingRet = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(strEncrypted));
|
|||
|
return PasswordEncodingRet;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static string FilterByString(ref string strFilter, ref string strColumnName, ref string strColumnValue)
|
|||
|
{
|
|||
|
string FilterByStringRet = default(string);
|
|||
|
// 對字串欄位做篩選
|
|||
|
if (string.IsNullOrEmpty(strFilter))
|
|||
|
{
|
|||
|
strFilter = strColumnName + " = '" + Strings.Replace(strColumnValue, "'", "''") + "'";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strFilter = strFilter + " And " + strColumnName + " = '" + Strings.Replace(strColumnValue, "'", "''") + "'";
|
|||
|
}
|
|||
|
FilterByStringRet = strFilter;
|
|||
|
return FilterByStringRet;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static string FilterByInteger(ref string strFilter, ref string strColumnName, ref int strColumnValue)
|
|||
|
{
|
|||
|
string FilterByIntegerRet = default(string);
|
|||
|
// 對數值欄位做篩選
|
|||
|
if (string.IsNullOrEmpty(strFilter))
|
|||
|
{
|
|||
|
strFilter = strColumnName + " = " + strColumnValue;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strFilter = strFilter + " And " + strColumnName + " = " + strColumnValue;
|
|||
|
}
|
|||
|
FilterByIntegerRet = strFilter;
|
|||
|
return FilterByIntegerRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string FilterByDate(ref string strFilter, ref string strColumnName, ref DateTime datFromDate, ref DateTime datEndDate)
|
|||
|
{
|
|||
|
string FilterByDateRet = default(string);
|
|||
|
// 對日期欄位做篩選
|
|||
|
if (string.IsNullOrEmpty(strFilter))
|
|||
|
{
|
|||
|
strFilter = strColumnName + " >= #" + Strings.Format(Conversions.ToDate(datFromDate), "yyyy/MM/dd 00:00:00") + "# And " + strColumnName + " <= #" + Strings.Format(Conversions.ToDate(datEndDate), "yyyy/MM/dd 23:59:59") + "# ";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strFilter = strFilter + " And " + strColumnName + " >= #" + Strings.Format(Conversions.ToDate(datFromDate), "yyyy/MM/dd 00:00:00") + "# And " + strColumnName + " <= #" + Strings.Format(Conversions.ToDate(datEndDate), "yyyy/MM/dd 23:59:59") + "# ";
|
|||
|
}
|
|||
|
FilterByDateRet = strFilter;
|
|||
|
return FilterByDateRet;
|
|||
|
}
|
|||
|
|
|||
|
public static bool IsBoolean(ref string strBoolean, string strBooleanValue = defString)
|
|||
|
{
|
|||
|
bool IsBooleanRet = default(bool);
|
|||
|
// 此 Function 檢查傳入值是否為Boolean值
|
|||
|
// 傳入值: strBoolean欲檢查是否為Boolean值的字串
|
|||
|
// strBooleanValue將檢查值轉換為True或False的統一字串
|
|||
|
// 傳回值: True是Boolean值 ; False不是Boolean值
|
|||
|
|
|||
|
IsBooleanRet = false;
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(strBoolean))
|
|||
|
{
|
|||
|
IsBooleanRet = false;
|
|||
|
}
|
|||
|
else if (Strings.Trim(Strings.UCase(strBoolean)) != "Y" & Strings.Trim(Strings.UCase(strBoolean)) != "N" & Strings.Trim(Strings.UCase(strBoolean)) != "T" & Strings.Trim(Strings.UCase(strBoolean)) != "F" & Strings.Trim(Strings.UCase(strBoolean)) != "YES" & Strings.Trim(Strings.UCase(strBoolean)) != "NO" & Strings.Trim(Strings.UCase(strBoolean)) != "TRUE" & Strings.Trim(Strings.UCase(strBoolean)) != "FALSE" & Strings.Trim(Strings.UCase(strBoolean)) != "是" & Strings.Trim(Strings.UCase(strBoolean)) != "否" & Strings.Trim(Strings.UCase(strBoolean)) != "ON" & Strings.Trim(Strings.UCase(strBoolean)) != "OFF")
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
{
|
|||
|
|
|||
|
IsBooleanRet = false;
|
|||
|
}
|
|||
|
|
|||
|
else if (Strings.Trim(Strings.UCase(strBoolean)) == "Y" | Strings.Trim(Strings.UCase(strBoolean)) == "T" | Strings.Trim(Strings.UCase(strBoolean)) == "YES" | Strings.Trim(Strings.UCase(strBoolean)) == "TRUE" | Strings.Trim(Strings.UCase(strBoolean)) == "是" | Strings.Trim(Strings.UCase(strBoolean)) == "ON")
|
|||
|
|
|||
|
{
|
|||
|
IsBooleanRet = true;
|
|||
|
strBooleanValue = "True";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
IsBooleanRet = true;
|
|||
|
strBooleanValue = "False";
|
|||
|
}
|
|||
|
|
|||
|
return IsBooleanRet;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static string CInput(ref string strInput)
|
|||
|
{
|
|||
|
string CInputRet = default(string);
|
|||
|
// 將傳入值內的單引號轉換為可存入資料庫的格式
|
|||
|
// 2. 將傳入值內的 &, >, < 三個特殊字元轉換為XmlDocument可解譯之代替符號
|
|||
|
// 傳入值: strInput包含特殊字元的字串
|
|||
|
// 傳回值: 將特殊字元變更為代替符號的字串
|
|||
|
|
|||
|
// 轉換 ' 為 '' (單引號轉為兩個單引號)
|
|||
|
CInputRet = Strings.Replace(strInput, "'", "''");
|
|||
|
|
|||
|
// 轉換 & 為 &
|
|||
|
CInputRet = Strings.Replace(CInputRet, "&", "&");
|
|||
|
|
|||
|
// CInput = Replace(CInput, """", "''") 'AddFlow的Xml字串不可將雙引號轉為兩個單引號,XMLToFlow會Error
|
|||
|
|
|||
|
// 轉換 > 為 >
|
|||
|
CInputRet = Strings.Replace(CInputRet, ">", ">");
|
|||
|
|
|||
|
// 轉換 < 為 <
|
|||
|
CInputRet = Strings.Replace(CInputRet, "<", "<");
|
|||
|
return CInputRet;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static string CUnInput(ref string strInput)
|
|||
|
{
|
|||
|
string CUnInputRet = default(string);
|
|||
|
// 將傳入值內的單引號轉換為可存入資料庫的格式
|
|||
|
// 傳入值: strInput包含特殊字元的字串
|
|||
|
// 傳回值: 將代替符號變更為特殊字元的字串
|
|||
|
|
|||
|
// 轉換 ' 為 '' (單引號轉為兩個單引號)
|
|||
|
// CUnInput = Replace(strInput, "'", "''")
|
|||
|
CUnInputRet = Strings.Replace(strInput, "\"", "'");
|
|||
|
|
|||
|
// 轉換 & 為 &
|
|||
|
CUnInputRet = Strings.Replace(CUnInputRet, "&", "&");
|
|||
|
|
|||
|
// 轉換 > 為 >
|
|||
|
CUnInputRet = Strings.Replace(CUnInputRet, ">", ">");
|
|||
|
|
|||
|
// 轉換 < 為 <
|
|||
|
CUnInputRet = Strings.Replace(CUnInputRet, "<", "<");
|
|||
|
return CUnInputRet;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// ***Add by PY 2003/07/31****
|
|||
|
public static string CombineXMLAdditional(ref string strAdditional)
|
|||
|
{
|
|||
|
string CombineXMLAdditionalRet = default(string);
|
|||
|
CombineXMLAdditionalRet = "<additional>" + strAdditional;
|
|||
|
CombineXMLAdditionalRet = CombineXMLAdditionalRet + "</additional>";
|
|||
|
return CombineXMLAdditionalRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineAddXML_Add(ref string name, ref string type, ref string value)
|
|||
|
{
|
|||
|
string CombineAddXML_AddRet = default(string);
|
|||
|
CombineAddXML_AddRet = "<field>" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "</field>";
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return CombineAddXML_AddRet;
|
|||
|
}
|
|||
|
public static string CombineAddXML_Edit(ref string name, ref string type, ref string value)
|
|||
|
{
|
|||
|
string CombineAddXML_EditRet = default(string);
|
|||
|
CombineAddXML_EditRet = "<field>" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "</field>";
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return CombineAddXML_EditRet;
|
|||
|
}
|
|||
|
public static string CombineAddXML_Field(ref string name)
|
|||
|
{
|
|||
|
string CombineAddXML_FieldRet = default(string);
|
|||
|
CombineAddXML_FieldRet = "<field>" + "<name>" + name + "</name>" + "</field>";
|
|||
|
|
|||
|
return CombineAddXML_FieldRet;
|
|||
|
}
|
|||
|
|
|||
|
public static string CombineAddXML_Condition(string condition)
|
|||
|
{
|
|||
|
string CombineAddXML_ConditionRet = default(string);
|
|||
|
CombineAddXML_ConditionRet = "<condition>" + Strings.Replace(condition, "''", "'") + "</condition>";
|
|||
|
return CombineAddXML_ConditionRet;
|
|||
|
}
|
|||
|
|
|||
|
// **Add by py 2003/12/04,Gary Lu 20120911:以MESWin1~Win4的版本進行替換
|
|||
|
public static string LocalizeWebService(string wsUrl, bool Customize = false)
|
|||
|
{
|
|||
|
|
|||
|
string[] tmpString;
|
|||
|
int i;
|
|||
|
tmpString = wsUrl.Split('/');
|
|||
|
if (Customize == true)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(Strings.Trim(gCUSWebServiceHost)))
|
|||
|
{
|
|||
|
if (tmpString.Length == 6)
|
|||
|
{
|
|||
|
wsUrl = tmpString[0].ToString() + "/" + tmpString[1].ToString() + "/" + gCUSWebServiceHost + "/" + tmpString[4].ToString() + "/" + tmpString[5].ToString();
|
|||
|
}
|
|||
|
else if (tmpString.Length == 5)
|
|||
|
{
|
|||
|
wsUrl = "http://" + gCUSWebServiceHost + "/" + tmpString[tmpString.Length - 1].ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else if (!string.IsNullOrEmpty(Strings.Trim(gMESWebServiceHost)))
|
|||
|
{
|
|||
|
if (tmpString.Length == 6)
|
|||
|
{
|
|||
|
// wsUrl = "http://" & gMESWebServiceHost & "/" & tmpString(tmpString.Length - 2).ToString & "/" & tmpString(tmpString.Length - 1).ToString
|
|||
|
wsUrl = tmpString[0].ToString() + "/" + tmpString[1].ToString() + "/" + gMESWebServiceHost + "/" + tmpString[4].ToString() + "/" + tmpString[5].ToString();
|
|||
|
}
|
|||
|
else if (tmpString.Length == 5)
|
|||
|
{
|
|||
|
// wsUrl = "http://" & gMESWebServiceHost & "/" & tmpString(tmpString.Length - 1).ToString
|
|||
|
wsUrl = tmpString[0].ToString() + "/" + tmpString[1].ToString() + "/" + gMESWebServiceHost + "/" + tmpString[4].ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
if (gEnableSSL)
|
|||
|
{
|
|||
|
wsUrl = wsUrl.Replace("http://", "https://");
|
|||
|
}
|
|||
|
return wsUrl;
|
|||
|
// Have to assume the web service is on the machine that this application came from
|
|||
|
} // LocalizeWebService
|
|||
|
|
|||
|
// Add By Peter 2005/6/20
|
|||
|
public static string GetAppSettings(string key, string section = "")
|
|||
|
{
|
|||
|
string result = string.Empty;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(section))
|
|||
|
{
|
|||
|
result = Conversions.ToString(((Hashtable)GetConfig(section))[key]);
|
|||
|
}
|
|||
|
else if (string.IsNullOrEmpty(gSettingMode) || string.IsNullOrEmpty(gSettingMode) || gSettingMode.Length == 0)
|
|||
|
{
|
|||
|
result = AppSettings[key];
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Hashtable ht = (Hashtable)GetConfig(gSettingMode);
|
|||
|
if (ht != null)
|
|||
|
{
|
|||
|
result = Conversions.ToString(ht[key]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static string GetExceptionStack(ref System.Xml.XmlDocument Xmldoc)
|
|||
|
{
|
|||
|
string GetExceptionStackRet = default(string);
|
|||
|
if (Xmldoc.DocumentElement.GetElementsByTagName("stack").Count > 0)
|
|||
|
{
|
|||
|
string argstrInput = Xmldoc.DocumentElement.GetElementsByTagName("stack").Item(0).InnerXml;
|
|||
|
GetExceptionStackRet = CUnInput(ref argstrInput);
|
|||
|
Xmldoc.DocumentElement.GetElementsByTagName("stack").Item(0).InnerXml = argstrInput;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
GetExceptionStackRet = "";
|
|||
|
}
|
|||
|
|
|||
|
return GetExceptionStackRet;
|
|||
|
}
|
|||
|
|
|||
|
public static void WriteLog(string msg, iMESLog.iMESLogLevel level, Exception e = null)
|
|||
|
{
|
|||
|
string MethodInfo = "";
|
|||
|
var ss = new StackTrace(true);
|
|||
|
var mb = ss.GetFrame(1).GetMethod();
|
|||
|
MethodInfo = mb.DeclaringType.Namespace + "." + mb.DeclaringType.Name + "." + mb.Name;
|
|||
|
|
|||
|
var log = new iMESLog.MESLog(mb.DeclaringType.Namespace);
|
|||
|
log.WriteLog(msg, level, e, MethodInfo);
|
|||
|
}
|
|||
|
#region 語系
|
|||
|
public static string TranslateMsg(string Message)
|
|||
|
{
|
|||
|
string TranslateMsgRet = default(string);
|
|||
|
gLanguageMode = GetAppSettings("LanguageMode");
|
|||
|
// 1.Check LanguageMode
|
|||
|
string strFileName, strGenFile;
|
|||
|
if (string.IsNullOrEmpty(gLanguageMode))
|
|||
|
{
|
|||
|
strFileName = "mesresource.en";
|
|||
|
strGenFile = "mesresource.en";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strFileName = "mesresource." + gLanguageMode;
|
|||
|
strGenFile = "mesresource." + gLanguageMode;
|
|||
|
}
|
|||
|
|
|||
|
// 2.Get File Path
|
|||
|
string strResourcePath = gSTDPath + "Resources";
|
|||
|
|
|||
|
string strErrName_Rep = "";
|
|||
|
|
|||
|
// 檢查檔案是否存在
|
|||
|
if (File.Exists(strResourcePath + "/" + strFileName + ".resources") == true)
|
|||
|
{
|
|||
|
// 4.Replace原始訊息.
|
|||
|
if (Strings.InStr(Message, "[%", CompareMethod.Text) > 0)
|
|||
|
{
|
|||
|
// Replace[%%]
|
|||
|
// Dim rmGen As Resources.ResourceManager = Resources.ResourceManager.CreateFileBasedResourceManager(strGenFile, strResourcePath, Nothing)
|
|||
|
|
|||
|
// 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
|
|||
|
ResourceManager ResourceMgr = null;
|
|||
|
Dictionary<string, string>[] ResourceDictionary = null;
|
|||
|
funGetMultiResourceManager(strResourcePath, gLanguageMode, ref ResourceMgr, ref ResourceDictionary);
|
|||
|
|
|||
|
string strKey, strKeyValue;
|
|||
|
int intStart, j, k, l;
|
|||
|
|
|||
|
intStart = 1;
|
|||
|
j = 1;
|
|||
|
|
|||
|
while (j != 0)
|
|||
|
{
|
|||
|
j = Strings.InStr(intStart, Message, "[%", CompareMethod.Text);
|
|||
|
if (j == 0)
|
|||
|
{
|
|||
|
strErrName_Rep += Strings.Mid(Message, intStart);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
k = Strings.InStr(j + 2, Message, "%]", CompareMethod.Text);
|
|||
|
if (k == 0)
|
|||
|
{
|
|||
|
// 找不到對應的結束字元.
|
|||
|
strErrName_Rep += Strings.Mid(Message, intStart);
|
|||
|
j = 0;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 檢查是否有不對稱的情況.
|
|||
|
l = Strings.InStr(j + 2, Message, "[%", CompareMethod.Text);
|
|||
|
if (l == 0 || l > k)
|
|||
|
{
|
|||
|
strErrName_Rep += Strings.Mid(Message, intStart, j - intStart);
|
|||
|
strKey = Strings.Mid(Message, j + 2, k - j - 2);
|
|||
|
// 將Key執行語系轉換
|
|||
|
try
|
|||
|
{
|
|||
|
// 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
|
|||
|
strKeyValue = funGetResourceManager_Text(strKey.ToUpper(), oResourceManager: ref ResourceMgr, oResourceDictionary: ref ResourceDictionary);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
strKeyValue = strKey;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(strKeyValue))
|
|||
|
{
|
|||
|
strErrName_Rep += strKey;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strErrName_Rep += strKeyValue;
|
|||
|
}
|
|||
|
// 下次截取字串的起始位置
|
|||
|
intStart = k + 2;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 不對稱,找到最內層的[%%]
|
|||
|
int m;
|
|||
|
// 在前面的條件下,一定找得到.
|
|||
|
m = Strings.InStrRev(Message, "[%", k, CompareMethod.Text);
|
|||
|
// 擷出不轉換的字串.
|
|||
|
strErrName_Rep += Strings.Mid(Message, intStart, m - intStart);
|
|||
|
strKey = Strings.Mid(Message, m + 2, k - m - 2);
|
|||
|
// 將Key執行語系轉換
|
|||
|
try
|
|||
|
{
|
|||
|
// 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
|
|||
|
strKeyValue = funGetResourceManager_Text(strKey.ToUpper(), oResourceManager: ref ResourceMgr, oResourceDictionary: ref ResourceDictionary);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
strKeyValue = strKey;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(strKeyValue))
|
|||
|
{
|
|||
|
strErrName_Rep += strKey;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strErrName_Rep += strKeyValue;
|
|||
|
}
|
|||
|
// 下次截取字串的起始位置
|
|||
|
intStart = k + 2;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
ResourceMgr = null;
|
|||
|
ResourceDictionary = null;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strErrName_Rep = Message;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strErrName_Rep = Message.Replace("[%", "").Replace("%]", "");
|
|||
|
}
|
|||
|
|
|||
|
TranslateMsgRet = strErrName_Rep;
|
|||
|
return TranslateMsgRet;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 2020/05/15 OwenLiu, Mantis:0071895, 取回所有語系檔案的 ResourceManager Array
|
|||
|
/// </summary>
|
|||
|
/// <param name="pResourcePath"></param>
|
|||
|
/// <param name="pLanguageMode"></param>
|
|||
|
/// <param name="oResourceManager"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static bool funGetMultiResourceManager(string pResourcePath, string pLanguageMode, ref ResourceManager oResourceManager, ref Dictionary<string, string>[] oResourceDictionary)
|
|||
|
{
|
|||
|
|
|||
|
bool blnResult = false;
|
|||
|
string strResourceFile = "mesresource";
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(pLanguageMode))
|
|||
|
{
|
|||
|
strResourceFile += "." + pLanguageMode;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
strResourceFile += ".en";
|
|||
|
}
|
|||
|
|
|||
|
if (new DirectoryInfo(pResourcePath).Exists == false)
|
|||
|
{
|
|||
|
oResourceManager = null;
|
|||
|
return blnResult;
|
|||
|
}
|
|||
|
|
|||
|
// 一次性建立多個ResourceManager, 以節省控制項的語系轉換須多次開檔的時間(控制項一定會有多個)
|
|||
|
int intIndex = 0;
|
|||
|
short intResouceFileQty = (short)new DirectoryInfo(pResourcePath).GetFiles(strResourceFile + ".resources*", SearchOption.TopDirectoryOnly).Count();
|
|||
|
|
|||
|
if (intResouceFileQty == 1)
|
|||
|
{
|
|||
|
oResourceDictionary = null;
|
|||
|
}
|
|||
|
else if (intResouceFileQty > 1)
|
|||
|
{
|
|||
|
oResourceDictionary = new Dictionary<string, string>[intResouceFileQty - 2 + 1];
|
|||
|
}
|
|||
|
foreach (string f in Directory.GetFiles(pResourcePath, strResourceFile + ".resources*", SearchOption.TopDirectoryOnly).OrderByDescending(x => x))
|
|||
|
{
|
|||
|
string ResourceBaseName = f.Substring(f.LastIndexOf(@"\") + 1);
|
|||
|
if (intIndex == intResouceFileQty - 1)
|
|||
|
{
|
|||
|
oResourceManager = ResourceManager.CreateFileBasedResourceManager(strResourceFile, pResourcePath, null);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 其他資源檔案 讀取到 Dictionary
|
|||
|
oResourceDictionary[intIndex] = funReadResourceFile_Dictionary(f);
|
|||
|
}
|
|||
|
|
|||
|
intIndex += 1;
|
|||
|
}
|
|||
|
blnResult = true;
|
|||
|
}
|
|||
|
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw;
|
|||
|
}
|
|||
|
|
|||
|
return blnResult;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 2020/05/15 OwenLiu, Mantis:0071895, 從所有語系檔案的 ResourceManager Array 取出對應的語系值
|
|||
|
/// </summary>
|
|||
|
/// <param name="pKey"></param>
|
|||
|
/// <param name="oResourceManager"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static string funGetResourceManager_Text(string pKey, ref ResourceManager oResourceManager, ref Dictionary<string, string>[] oResourceDictionary)
|
|||
|
{
|
|||
|
string strResult = "";
|
|||
|
bool blnKeyExists = false;
|
|||
|
|
|||
|
do
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
// 先搜尋Dictionary 是否有對應的Key
|
|||
|
if (oResourceDictionary != null)
|
|||
|
{
|
|||
|
foreach (Dictionary<string, string> ResourceDic in oResourceDictionary)
|
|||
|
{
|
|||
|
if (ResourceDic.ContainsKey(pKey))
|
|||
|
{
|
|||
|
strResult = ResourceDic[pKey];
|
|||
|
blnKeyExists = true;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
if (!blnKeyExists)
|
|||
|
{
|
|||
|
// 未找到Key, 搜尋ResourceManager
|
|||
|
strResult = oResourceManager.GetString(pKey);
|
|||
|
if (!string.IsNullOrEmpty(strResult))
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
}
|
|||
|
}
|
|||
|
// Throw
|
|||
|
while (false);
|
|||
|
|
|||
|
return strResult;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 2020/05/17 OwenLiu, Mantis:0071895, 讀取語系檔案到 Dictionary
|
|||
|
/// </summary>
|
|||
|
/// <param name="ResourceFilePath"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static Dictionary<string, string> funReadResourceFile_Dictionary(string ResourceFilePath)
|
|||
|
{
|
|||
|
|
|||
|
var oReturnResource = new Dictionary<string, string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var reader = new ResourceReader(ResourceFilePath);
|
|||
|
var en = reader.GetEnumerator();
|
|||
|
|
|||
|
while (en.MoveNext())
|
|||
|
oReturnResource.Add(Conversions.ToString(en.Key), Conversions.ToString(en.Value));
|
|||
|
reader.Close();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw;
|
|||
|
}
|
|||
|
|
|||
|
return oReturnResource;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|