|
|
@@ -1,14 +1,1845 @@
|
|
|
-using System;
|
|
|
+using OTSDataType;
|
|
|
+using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Management;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.Windows.Forms;
|
|
|
+using static OTSDataType.otsdataconst;
|
|
|
|
|
|
namespace OTSModelSharp
|
|
|
{
|
|
|
- public class OTSLicMgr
|
|
|
+ public class COTSLicMgr
|
|
|
{
|
|
|
+ // file pathname
|
|
|
+ public string m_strPathName = "";
|
|
|
+
|
|
|
+ // modify flag
|
|
|
+ bool m_bModify;
|
|
|
+
|
|
|
+ // license status
|
|
|
+ OTS_LICENSE_STATUS m_nLicStatus;
|
|
|
+
|
|
|
+ // license info list
|
|
|
+ List<COTSLicenseInfo> m_listLicenseInfo;
|
|
|
+
|
|
|
+ const String LICENSE_FILENAME = "OTSlicense.lcs";
|
|
|
+
|
|
|
+ // machine id string length
|
|
|
+ const int MACHINEID_STR_LEN = 19;
|
|
|
+
|
|
|
+ // software package id
|
|
|
+ OTS_SOFT_PACKAGE_ID m_nPackId;
|
|
|
+
|
|
|
+ // software package license file
|
|
|
+ COTSLicenseFile m_pOTSLicenseFile;
|
|
|
+
|
|
|
+ // close overdue days
|
|
|
+ const int CLOSE_OVERDUE_DAYS = 5;
|
|
|
+
|
|
|
+ // license into file name
|
|
|
+ const String LICENSE_INFO_FILE_NAME = "OTSlicenseInfo.lsf";
|
|
|
+
|
|
|
+
|
|
|
+ // software package license file
|
|
|
+ COTSLicInfoFile m_pOTSLicenseInfoFile;
|
|
|
+
|
|
|
+ // license info
|
|
|
+ COTSLicenseInfo m_pLicenseInfo;
|
|
|
+
|
|
|
+ const String LINE_END = "\r\n";
|
|
|
+
|
|
|
+ // expire date string length
|
|
|
+ const int EXPIRE_DATE_STR_LENGTH = 8;
|
|
|
+
|
|
|
+ // key string
|
|
|
+ const String CRYTP_KEY_STRING = "OPTON_OTS_PACKAGE";
|
|
|
+
|
|
|
+ const int LICENSE_STR_MIN_LENGTH = MACHINEID_STR_LEN + 1 + 1 + EXPIRE_DATE_STR_LENGTH + 1;
|
|
|
+
|
|
|
+ // expire date format string
|
|
|
+ const String EXPIRE_DATA_FORMAT = "%d%m%Y";
|
|
|
+
|
|
|
+ const int TEXTFILE_ITEM_COLUMN_NUMBER = 2;
|
|
|
+
|
|
|
+ // constructor
|
|
|
+ public void OTSLicenseFile()
|
|
|
+ {
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+
|
|
|
+ // initialization
|
|
|
+ public void Init()
|
|
|
+ {
|
|
|
+ m_strPathName = "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // license info list
|
|
|
+ public List<COTSLicenseInfo> GetLicenseInfoList()
|
|
|
+ { return m_listLicenseInfo; }
|
|
|
+
|
|
|
+ public void OTSLicMgr(COTSLicenseInfo a_oSource)
|
|
|
+ {
|
|
|
+
|
|
|
+ Duplicate(a_oSource);
|
|
|
+ }
|
|
|
+
|
|
|
+ public COTSLicMgr(COTSLicMgr a_poSource)
|
|
|
+ {
|
|
|
+
|
|
|
+ // can't copy itself
|
|
|
+ if (a_poSource == this)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // duplication
|
|
|
+ public void Duplicate(COTSLicenseInfo a_oSource)
|
|
|
+ {
|
|
|
+ // initialization
|
|
|
+ Init();
|
|
|
+
|
|
|
+ // copy data over
|
|
|
+ foreach (var pLicenseInfoSource in m_listLicenseInfo)
|
|
|
+ {
|
|
|
+ COTSLicenseInfo pLicenseInfoNew = new COTSLicenseInfo(pLicenseInfoSource);
|
|
|
+ m_listLicenseInfo.Add(pLicenseInfoNew);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ public String GetPathName() { return m_strPathName; }
|
|
|
+
|
|
|
+ public void SetPathName(String a_strPathName) { m_strPathName = a_strPathName; }
|
|
|
+
|
|
|
+ // modify flag
|
|
|
+ public bool IsModified() { return m_bModify; }
|
|
|
+
|
|
|
+ public void SetModify(bool a_bModify = true) { m_bModify = a_bModify; }
|
|
|
+
|
|
|
+
|
|
|
+ // check if there is valid license of the given software package
|
|
|
+ public bool IsThereValidPackLicense(OTS_SOFT_PACKAGE_ID a_nPackId)
|
|
|
+ {
|
|
|
+ // make sure software package id is valid
|
|
|
+ if (a_nPackId == OTS_SOFT_PACKAGE_ID.INVALID)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get software package license file pathname
|
|
|
+ String strFilePathName = GetPackLicenseFilePathName(a_nPackId);
|
|
|
+ strFilePathName.Trim();
|
|
|
+ if (strFilePathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ m_nLicStatus = OTS_LICENSE_STATUS.INVALID;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // create a license file pointer
|
|
|
+ COTSLicenseFile pOTSLicenseFilePtr = new COTSLicenseFile();
|
|
|
+
|
|
|
+ // load license file
|
|
|
+ if (!pOTSLicenseFilePtr.Load(strFilePathName))
|
|
|
+ {
|
|
|
+ // failed to load the software package license file
|
|
|
+
|
|
|
+ m_nLicStatus = OTS_LICENSE_STATUS.NO_FILE;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ m_pOTSLicenseFile = pOTSLicenseFilePtr;
|
|
|
+ m_nPackId = a_nPackId;
|
|
|
+
|
|
|
+ // return the license file validation result
|
|
|
+ return IsValidLicense(a_nPackId, m_listLicenseInfo[0], m_nLicStatus, false, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // get software package license file pathname
|
|
|
+ public String GetPackLicenseFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
|
|
|
+ {
|
|
|
+
|
|
|
+ // get software package system data pathname
|
|
|
+ String strOTSPackSysDataPathName = GetOTSPackSysDataPathName(a_nPackId);//OTSTools中的类改完再改这个方法
|
|
|
+
|
|
|
+ // check if software package system data pathname is right
|
|
|
+ if (strOTSPackSysDataPathName == "")
|
|
|
+ {
|
|
|
+ // failed to get software package system data pathname
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // software package license file pathname
|
|
|
+ // i.e. "c:\ProgramData\OPTON\OTSIncA\SysData\OTSLicense.lic"
|
|
|
+ String strOTSPackLicFileNathName = strOTSPackSysDataPathName + LICENSE_FILENAME;
|
|
|
+
|
|
|
+ // return software package license file pathname
|
|
|
+ return strOTSPackLicFileNathName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get software pack system data path
|
|
|
+ public String GetOTSPackSysDataPathName(OTS_SOFT_PACKAGE_ID a_nPackId)//deprecated,since we have build one new solution for the particle system.
|
|
|
+ {
|
|
|
+ // get app package name
|
|
|
+ String strAppPackageName = "";
|
|
|
+ switch (a_nPackId)
|
|
|
+ {
|
|
|
+ case OTS_SOFT_PACKAGE_ID.OTSIncA:
|
|
|
+ strAppPackageName = DataPublic.STR_APPNAME_OTSINCA;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case OTS_SOFT_PACKAGE_ID.OTSPartA:
|
|
|
+ strAppPackageName = DataPublic.STR_APPNAME_OTSINCA;
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // get common data pathname string
|
|
|
+ String strCommonDataPathName = GetOSCommonDataPathName();
|
|
|
+ if (strCommonDataPathName == "")
|
|
|
+ {
|
|
|
+ // can't common data path
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // software package system data pathname
|
|
|
+ // e.g. "c:\ProgramData\OPTON\OTSIncA\SysData\"
|
|
|
+ //CString strOTSSysDataPathName = strCommonDataPathName + STR_COFIGPATH + _T("\\") + strAppPackageName + _T("\\") + STR_SYSTEM_DATA + _T("\\");
|
|
|
+ String strOTSSysDataPathName = strCommonDataPathName + DataPublic.STR_COFIGPATH + "\\" + DataPublic.STR_SYSTEM_DATA + "\\";
|
|
|
+
|
|
|
+ // return software package system data path
|
|
|
+ return strOTSSysDataPathName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get system common data folder pathname
|
|
|
+ // return "" if failed
|
|
|
+ public String GetOSCommonDataPathName()
|
|
|
+ {
|
|
|
+
|
|
|
+ String strPathName = ".\\";
|
|
|
+
|
|
|
+
|
|
|
+ return strPathName;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // check if COTSLicenseInfo is valid license info
|
|
|
+ public bool IsValidLicense(OTS_SOFT_PACKAGE_ID a_nPackId,
|
|
|
+ COTSLicenseInfo a_poLicenseInfo,
|
|
|
+ OTS_LICENSE_STATUS a_nLicStatus,
|
|
|
+ bool a_bCheckMachinId /*= TRUE*/,
|
|
|
+ bool a_bTraceInfo /* = TRUE*/)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ // computer nick name
|
|
|
+ String strComputerNickName = a_poLicenseInfo.GetComputerNickName();
|
|
|
+ strComputerNickName.Trim();
|
|
|
+ if (strComputerNickName == "")
|
|
|
+ {
|
|
|
+ // nick name is empty
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.COMPUTER_NICK_NAME_EMPTY;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // machine id
|
|
|
+ String strMachineId = a_poLicenseInfo.GetMachineId();
|
|
|
+ strMachineId.Trim();
|
|
|
+ if (strMachineId.Length != MACHINEID_STR_LEN)
|
|
|
+ {
|
|
|
+ // invalid machine id string
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.INVALID_FILE;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else if (a_bCheckMachinId)
|
|
|
+ {
|
|
|
+ String strLocalMachinId = GetDiskSerialNumber();
|
|
|
+ if (strMachineId.CompareTo(strLocalMachinId) != 0)
|
|
|
+ {
|
|
|
+ // machine id not match
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.MACHINEID_NOT_MATCH;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // software package id
|
|
|
+ if (a_poLicenseInfo.GetPackId() < (int)OTS_SOFT_PACKAGE_ID.MIN || (int)a_poLicenseInfo.GetPackId() > (int)OTS_SOFT_PACKAGE_ID.MAX)
|
|
|
+ {
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.INVALID_FILE;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ else if ((int)a_nPackId != (int)a_poLicenseInfo.GetPackId())
|
|
|
+ {
|
|
|
+ // software package id not match
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.SOFTWARE_PACKID_NOT_MATCH;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // license type
|
|
|
+ if (a_poLicenseInfo.GetLicType() < (int)OTS_LICENSE_TYPE.MIN || (int)a_poLicenseInfo.GetLicType() > (int)OTS_LICENSE_TYPE.MAX)
|
|
|
+ {
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.INVALID_FILE;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // expire date
|
|
|
+ DateTime.DaysInMonth(1, 1);
|
|
|
+ DateTime timeCurrent = new DateTime();
|
|
|
+ if (timeCurrent >= a_poLicenseInfo.GetExpireDate())
|
|
|
+ {
|
|
|
+ // expired
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.EXPIRED;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // is close to expire?
|
|
|
+ TimeSpan oLeftTime = a_poLicenseInfo.GetExpireDate() - timeCurrent;
|
|
|
+ int nLeftDays = oLeftTime.Days;
|
|
|
+ if (nLeftDays <= CLOSE_OVERDUE_DAYS)
|
|
|
+ {
|
|
|
+ // close to overdue
|
|
|
+ if (a_bTraceInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.CLOSE_OVERDUE;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ok, return TRUE
|
|
|
+ a_nLicStatus = OTS_LICENSE_STATUS.VALID;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get machine id
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 取得设备硬盘的物理序列号
|
|
|
+
|
|
|
+ public static string GetDiskSerialNumber()
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ ManagementObjectSearcher mos = new ManagementObjectSearcher();
|
|
|
+
|
|
|
+ mos.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID", "Signature" });
|
|
|
+
|
|
|
+ ManagementObjectCollection myCollection = mos.Get();
|
|
|
+
|
|
|
+ ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator();
|
|
|
+
|
|
|
+ em.MoveNext();
|
|
|
+
|
|
|
+ ManagementBaseObject moo = em.Current;
|
|
|
+
|
|
|
+ string id = moo.Properties["signature"].Value.ToString().Trim();
|
|
|
+
|
|
|
+ return id;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public COTSLicenseFile GetOTSLicenseFile() { return m_pOTSLicenseFile; }
|
|
|
+
|
|
|
+ public bool LoadLicenseInfoFile()
|
|
|
+ {
|
|
|
+ // create a license info file pointer
|
|
|
+ COTSLicInfoFile pOTSLicInfoFilePtr = new COTSLicInfoFile();
|
|
|
+
|
|
|
+ // get company log path
|
|
|
+ String strCompanySysDataPathName = GetSysDataPathName();
|
|
|
+ if (strCompanySysDataPathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get license info file pathname
|
|
|
+ String strLicenseInfoPathName = GetLicenseInfoFilePathName();
|
|
|
+ if (strLicenseInfoPathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if company system data path exist
|
|
|
+ if (Exists(strCompanySysDataPathName))
|
|
|
+ {
|
|
|
+ // yes, try to open license info file pathname
|
|
|
+
|
|
|
+ // check if the file is there
|
|
|
+ if (!Exists(strLicenseInfoPathName))
|
|
|
+ {
|
|
|
+ // license info file is missing
|
|
|
+
|
|
|
+ pOTSLicInfoFilePtr.SetPathName(strLicenseInfoPathName);
|
|
|
+ }
|
|
|
+ // load license info file
|
|
|
+ else if (!pOTSLicInfoFilePtr.Load(strLicenseInfoPathName))
|
|
|
+ {
|
|
|
+ // failed to load the license info file
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // create company system data path
|
|
|
+ else if (CreateFolder(strCompanySysDataPathName))
|
|
|
+ {
|
|
|
+ // company system data path is missing
|
|
|
+
|
|
|
+ pOTSLicInfoFilePtr.SetPathName(strLicenseInfoPathName);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // failed to create company system data path
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // assign license info file point
|
|
|
+ m_pOTSLicenseInfoFile = pOTSLicInfoFilePtr;
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public COTSLicInfoFile GetOTSLicenseInfoFile() { return m_pOTSLicenseInfoFile; }
|
|
|
+
|
|
|
+ // creates a folder.
|
|
|
+ public bool CreateFolder(string a_strFolder)
|
|
|
+ {
|
|
|
+ // make sure the folder name string are not empty
|
|
|
+ String strFolder = a_strFolder;
|
|
|
+ strFolder.Trim();
|
|
|
+ if (strFolder == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // if the folder exist?
|
|
|
+ if (Exists(strFolder))
|
|
|
+ {
|
|
|
+ // is a real folder?
|
|
|
+ if (IsFolder(strFolder))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // create folder
|
|
|
+
|
|
|
+ // remove back slash if there
|
|
|
+ String strParentFolder = strFolder;
|
|
|
+ strParentFolder.Trim();
|
|
|
+
|
|
|
+ // find last back slash position
|
|
|
+ int nBackSlashPos = Convert.ToInt32(strParentFolder);
|
|
|
+ if (nBackSlashPos > 0)
|
|
|
+ {
|
|
|
+ // get the folder name
|
|
|
+ String strFolderName = strParentFolder.PadRight(strParentFolder.Length - nBackSlashPos - 1);
|
|
|
+
|
|
|
+ // separate the folder string
|
|
|
+ strParentFolder = strParentFolder.PadLeft(nBackSlashPos);
|
|
|
+ if (!IsValidFileName(strFolderName))
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // try to create folder from the base folder
|
|
|
+ if (!CreateFolder(strParentFolder))
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // create the folder
|
|
|
+ bool bRet = true;
|
|
|
+
|
|
|
+ // return folder create result
|
|
|
+ return bRet;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if the given string is valid file name or not
|
|
|
+ public bool IsValidFileName(string a_sFileName)
|
|
|
+ {
|
|
|
+ String strFileName = a_sFileName;
|
|
|
+ const String INVALIDFILENAMECHAR = "\\/:*?\"<>";
|
|
|
+ a_sFileName = INVALIDFILENAMECHAR;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if this is an existing folder
|
|
|
+ public bool IsFolder(string a_strFolder)
|
|
|
+ {
|
|
|
+ if (a_strFolder != "")
|
|
|
+ {
|
|
|
+ return HasAttribute(a_strFolder, DataPublic.FILE_ATTRIBUTE_DIRECTORY);
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool HasAttribute(string a_strFolder, int a_nAttribute)
|
|
|
+ {
|
|
|
+ int flags = Convert.ToInt32(a_strFolder);
|
|
|
+ return (flags != DataPublic.INVALID_FILE_ATTRIBUTES) && (flags == a_nAttribute);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // check if the file exists or not
|
|
|
+ public bool Exists(string a_sPath)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get license info file pathname
|
|
|
+ public String GetLicenseInfoFilePathName()
|
|
|
+ {
|
|
|
+ // get company system data path
|
|
|
+ String strOTSSysDataPathName = GetSysDataPathName();
|
|
|
+
|
|
|
+ // check if software package system data pathname is right
|
|
|
+ if (strOTSSysDataPathName == "")
|
|
|
+ {
|
|
|
+ // failed to get company system data pathname
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // license info file pathname
|
|
|
+ // i.e. "c:\ProgramData\OPTON\SysData\OTSLicenseInfo.lcf"
|
|
|
+ String strLicenseInfoFileNathName = strOTSSysDataPathName + LICENSE_INFO_FILE_NAME;
|
|
|
+
|
|
|
+ return strLicenseInfoFileNathName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get company system data path
|
|
|
+ public String GetSysDataPathName()
|
|
|
+ {
|
|
|
+ // get common data pathname string
|
|
|
+ String strCommonDataPathName = GetOSCommonDataPathName();
|
|
|
+ if (strCommonDataPathName == "")
|
|
|
+ {
|
|
|
+ // failed to get common data pathname string
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // company system data pathname
|
|
|
+ // e.g. "c:\ProgramData\OPTON\SysData\"
|
|
|
+ String strCmpSysDataPath = strCommonDataPathName + DataPublic.STR_COFIGPATH + "\\" + DataPublic.STR_SYSTEM_DATA + "\\";
|
|
|
+
|
|
|
+ // return company system data pathname
|
|
|
+ return strCmpSysDataPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ // license info
|
|
|
+ public COTSLicenseInfo GetLicenseInfo() { return m_pLicenseInfo; }
|
|
|
+
|
|
|
+ // load a license file (license key file)
|
|
|
+ public COTSLicenseInfo LoadLicenseInfoFromFile()
|
|
|
+ {
|
|
|
+ // file pathname
|
|
|
+ String strFilePathName = "";
|
|
|
+
|
|
|
+
|
|
|
+ // check if file pathname is empty
|
|
|
+ m_strPathName.Trim();
|
|
|
+ if (m_strPathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ m_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(m_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(m_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+ // create a license file pointer
|
|
|
+ COTSLicenseInfo pOTSLicenseFilePtr = new COTSLicenseInfo();
|
|
|
+ if (!Load(strFilePathName))
|
|
|
+ {
|
|
|
+ // failed to load the file
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // return license info
|
|
|
+ return pOTSLicenseFilePtr.GetLicenseInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public bool Load(String a_strPathName)
|
|
|
+ {
|
|
|
+ // check the file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ a_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(a_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(a_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // gets the name of the folder
|
|
|
+ public String GetFolderName(String a_strPathName)
|
|
|
+ {
|
|
|
+ Char[] sPath = new char[ DataPublic.MAX_PATH];
|
|
|
+
|
|
|
+ String.Format(sPath.ToString(), DataPublic.MAX_PATH.ToString(), a_strPathName);
|
|
|
+
|
|
|
+ // PathRemoveFileSpec(sPath);
|
|
|
+
|
|
|
+ // 1、首先判断文件或者文件路径是否存在
|
|
|
+ if (File.Exists(sPath.ToString()))
|
|
|
+ {
|
|
|
+ // 2、根据路径字符串判断是文件还是文件夹
|
|
|
+ FileAttributes attr = File.GetAttributes(sPath.ToString());
|
|
|
+ // 3、根据具体类型进行删除
|
|
|
+ if (attr == FileAttributes.Directory)
|
|
|
+ {
|
|
|
+ // 3.1、删除文件夹
|
|
|
+ Directory.Delete(sPath.ToString(), true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 3.2、删除文件
|
|
|
+ File.Delete(sPath.ToString());
|
|
|
+ }
|
|
|
+ File.Delete(sPath.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ return sPath.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ //public bool Save(String a_strPathName)
|
|
|
+ // {
|
|
|
+ // // check the file pathname
|
|
|
+ // a_strPathName.Trim();
|
|
|
+ // if (a_strPathName=="")
|
|
|
+ // {
|
|
|
+
|
|
|
+ // return false;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // get path of the file
|
|
|
+ // String strFilePath = GetFolderName(a_strPathName);
|
|
|
+ // if (strFilePath!="")
|
|
|
+ // {
|
|
|
+ // // check if file folder exists
|
|
|
+ // if (!Exists(strFilePath))
|
|
|
+ // {
|
|
|
+ // // create the file folder
|
|
|
+ // if (!CreateFolder(strFilePath))
|
|
|
+ // {
|
|
|
+ // // failed to create the file folder
|
|
|
+
|
|
|
+ // return false;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // if (!File.Exists(a_strPathName))
|
|
|
+ // {
|
|
|
+ // // failed to open the file
|
|
|
+ // return false;
|
|
|
+ // }
|
|
|
+
|
|
|
+
|
|
|
+ // FileStream NewText = File.Create(a_strPathName);
|
|
|
+ // NewText.Close();
|
|
|
+
|
|
|
+
|
|
|
+ // // file pathname
|
|
|
+ // m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // // ok, return TRUE
|
|
|
+ // return true;
|
|
|
+
|
|
|
+ // }
|
|
|
+
|
|
|
+ public bool Save(String a_strPathName)
|
|
|
+ {
|
|
|
+ // check the file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get path of the file
|
|
|
+ String strFilePath = GetFolderName(a_strPathName);
|
|
|
+ if (strFilePath != "")
|
|
|
+ {
|
|
|
+ // check if file folder exists
|
|
|
+ if (strFilePath != "")
|
|
|
+ {
|
|
|
+ // create the file folder
|
|
|
+ if (CreateFolder(strFilePath))
|
|
|
+ {
|
|
|
+ // failed to create the file folder
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // check file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ a_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+ // open the particle analysis standard file
|
|
|
+
|
|
|
+ if (!File.Exists(a_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(a_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // constructor
|
|
|
+ public void COTSLicInfoFile()
|
|
|
+ {
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // is valid machine id string
|
|
|
+ public bool IsValidMachineIdString(String a_strMachineId)
|
|
|
+ {
|
|
|
+ a_strMachineId.Trim();
|
|
|
+ return (a_strMachineId != "" && a_strMachineId.Length == MACHINEID_STR_LEN);
|
|
|
+ }
|
|
|
+
|
|
|
+ // get license info text body
|
|
|
+ public String GetLicenseInfoTextBody(COTSLicenseInfo a_pOTSLicenseInfo)
|
|
|
+ {
|
|
|
+ // license info text body
|
|
|
+ String strLicenseInfoTextBody = "";
|
|
|
+
|
|
|
+ String strTitle = "";
|
|
|
+
|
|
|
+ // nick name
|
|
|
+ strTitle = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSE_INFO_TITLE_FIRST);
|
|
|
+ strLicenseInfoTextBody = strTitle + DataPublic.FILE_TITLE_SPLIT + " " + a_pOTSLicenseInfo.GetComputerNickName() + LINE_END;
|
|
|
+ // machine id
|
|
|
+ strTitle = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSE_INFO_TITLE_FIRST + 1);
|
|
|
+ strLicenseInfoTextBody += strTitle + DataPublic.FILE_TITLE_SPLIT + " " + a_pOTSLicenseInfo.GetMachineId() + LINE_END;
|
|
|
+ // software package id
|
|
|
+ strTitle = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSE_INFO_TITLE_FIRST + 2);
|
|
|
+ String strPackId = String.Format("%i", (int)a_pOTSLicenseInfo.GetPackId());
|
|
|
+ strLicenseInfoTextBody += strTitle + DataPublic.FILE_TITLE_SPLIT + " " + strPackId + LINE_END;
|
|
|
+ // license type id
|
|
|
+ strTitle = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSE_INFO_TITLE_FIRST + 3);
|
|
|
+ String strLicType;
|
|
|
+ strLicType = String.Format("%i", (int)a_pOTSLicenseInfo.GetLicType());
|
|
|
+ strLicenseInfoTextBody += strTitle + DataPublic.FILE_TITLE_SPLIT + " " + strLicType + LINE_END;
|
|
|
+ // expire date
|
|
|
+ strTitle = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSE_INFO_TITLE_FIRST + 4);
|
|
|
+ String strExpiredDate;
|
|
|
+ strExpiredDate = String.Format(EXPIRE_DATA_FORMAT);
|
|
|
+ strLicenseInfoTextBody += strTitle + DataPublic.FILE_TITLE_SPLIT + " " + strExpiredDate + LINE_END;
|
|
|
+
|
|
|
+ // return license info text body
|
|
|
+ return strLicenseInfoTextBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ public COTSLicenseInfo DecryptLicenseInfo(String a_strLicense)
|
|
|
+ {
|
|
|
+
|
|
|
+ // make sure the license string is valid
|
|
|
+ a_strLicense.Trim();
|
|
|
+ if (a_strLicense == "")
|
|
|
+ {
|
|
|
+ // invalid license string
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // decrypt the license string
|
|
|
+ OTSCrypt oCrypt = new OTSCrypt();
|
|
|
+ String strKey = CRYTP_KEY_STRING;
|
|
|
+
|
|
|
+ oCrypt.Decrypt(strKey, a_strLicense);
|
|
|
+
|
|
|
+ // make sure license string is longer enough
|
|
|
+ if (a_strLicense.Length < LICENSE_STR_MIN_LENGTH)
|
|
|
+ {
|
|
|
+ // invalid license string
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // separate the license string
|
|
|
+ // computer machine id string -- length MACHINEID_STR_LEN (19)
|
|
|
+ // software package id string -- length 1
|
|
|
+ // license type id string -- length 1
|
|
|
+ // expired date string -- EXPIRED_DATE_STR_LENGTH (8)
|
|
|
+ // computer nick name
|
|
|
+
|
|
|
+
|
|
|
+ String strMachineId = a_strLicense.PadLeft(MACHINEID_STR_LEN);
|
|
|
+ String strPackId = a_strLicense.Substring(MACHINEID_STR_LEN, 1);
|
|
|
+ String strLicTypeId = a_strLicense.Substring(MACHINEID_STR_LEN + 1, 1);
|
|
|
+ String strExpiredDate = a_strLicense.Substring(MACHINEID_STR_LEN + 1 + 1, EXPIRE_DATE_STR_LENGTH);
|
|
|
+ int nComputerNickNameStrLength = a_strLicense.Length - MACHINEID_STR_LEN - 1 - 1 - EXPIRE_DATE_STR_LENGTH;
|
|
|
+ String strComputerNickName = a_strLicense.PadRight(nComputerNickNameStrLength);
|
|
|
+
|
|
|
+ // create an license info
|
|
|
+ COTSLicenseInfo pOTSLicenseInfo = new COTSLicenseInfo();
|
|
|
+
|
|
|
+ // assign machine id
|
|
|
+ strMachineId.Trim();
|
|
|
+ if (strMachineId.Length != MACHINEID_STR_LEN)
|
|
|
+ {
|
|
|
+ // invalid machine id
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ pOTSLicenseInfo.SetMachineId(strMachineId);
|
|
|
+
|
|
|
+ // assign software package id
|
|
|
+ int nPackId = 0;
|
|
|
+ if (!StringToInt(strPackId, nPackId) ||
|
|
|
+ (OTS_SOFT_PACKAGE_ID)nPackId < OTS_SOFT_PACKAGE_ID.MIN ||
|
|
|
+ (OTS_SOFT_PACKAGE_ID)nPackId > OTS_SOFT_PACKAGE_ID.MAX)
|
|
|
+ {
|
|
|
+ // invalid package id value
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ pOTSLicenseInfo.SetPackId((OTS_SOFT_PACKAGE_ID)nPackId);
|
|
|
+
|
|
|
+ // assign license type
|
|
|
+ int nLicType = 0;
|
|
|
+ if (!StringToInt(strLicTypeId, nLicType) ||
|
|
|
+ (OTS_LICENSE_TYPE)nLicType < OTS_LICENSE_TYPE.MIN ||
|
|
|
+ (OTS_LICENSE_TYPE)nLicType > OTS_LICENSE_TYPE.MAX)
|
|
|
+ {
|
|
|
+ // invalid license type value
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ pOTSLicenseInfo.SetLicType((OTS_LICENSE_TYPE)nLicType);
|
|
|
+
|
|
|
+ // assign expire data
|
|
|
+ // Expire data format "%d%m%Y"
|
|
|
+ if (!IsDigitString(strExpiredDate))
|
|
|
+ {
|
|
|
+ // invalid expire data string
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String strDay = strExpiredDate.PadLeft(2);
|
|
|
+ int nDay = 0;
|
|
|
+ StringToInt(strDay, nDay);
|
|
|
+ String strMonth = strExpiredDate.Substring(2, 2);
|
|
|
+ int nMonth = 0;
|
|
|
+ StringToInt(strMonth, nMonth);
|
|
|
+ String strYear = strExpiredDate.PadRight(4);
|
|
|
+ int nYear = 0;
|
|
|
+ StringToInt(strYear, nYear);
|
|
|
+ DateTime oDataTime = new DateTime();
|
|
|
+ oDataTime.AddDays(nDay);
|
|
|
+ oDataTime.AddMonths(nMonth);
|
|
|
+ oDataTime.AddYears(nYear);
|
|
|
+ pOTSLicenseInfo.SetExpireDate(oDataTime);
|
|
|
+
|
|
|
+ // assign computer nick name
|
|
|
+ strComputerNickName.Trim();
|
|
|
+ if (strComputerNickName == "")
|
|
|
+ {
|
|
|
+ // invalid computer nick name
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ pOTSLicenseInfo.SetComputerNickName(strComputerNickName);
|
|
|
+
|
|
|
+ // return the license info
|
|
|
+ return pOTSLicenseInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Strings to int.
|
|
|
+ public bool StringToInt(string a_sValue, int a_nValue)
|
|
|
+ {
|
|
|
+ if (a_sValue != "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ a_nValue = Convert.ToInt32(a_sValue);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Determines whether is digit string.
|
|
|
+ public bool IsDigitString(string a_sValue)
|
|
|
+ {
|
|
|
+ String strInt = a_sValue;
|
|
|
+ strInt.Trim();
|
|
|
+
|
|
|
+ if (strInt == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ int nStart = 0;
|
|
|
+
|
|
|
+ if (strInt[nStart] == '-')
|
|
|
+ {
|
|
|
+ ++nStart;
|
|
|
+ }
|
|
|
+
|
|
|
+ // cycle through string and check each character if it is a digit
|
|
|
+ for (nStart = 0; nStart < strInt.Length; ++nStart)
|
|
|
+ {
|
|
|
+ if (strInt[nStart] != '0')
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // create an encrypt string containing the given info
|
|
|
+ public String EncryptLicenseInfo(COTSLicenseInfo a_pOTSLicenseInfo)
|
|
|
+ {
|
|
|
+ // license string
|
|
|
+ // computer machine id string -- length MACHINEID_STR_LEN (19)
|
|
|
+ // software package id string -- length 1
|
|
|
+ // license type id string -- length 1
|
|
|
+ // expired date string -- length 8
|
|
|
+ // computer nick name
|
|
|
+ String strPackId;
|
|
|
+ strPackId = String.Format("%i", (int)a_pOTSLicenseInfo.GetPackId());
|
|
|
+ String strLicType = "";
|
|
|
+ String.Format("%i", (int)a_pOTSLicenseInfo.GetLicType());
|
|
|
+ String[] strExpiredDate;
|
|
|
+ strExpiredDate = a_pOTSLicenseInfo.GetExpireDate().GetDateTimeFormats();
|
|
|
+ String strLicense;
|
|
|
+ strLicense = a_pOTSLicenseInfo.GetMachineId() + strPackId + strLicType + strExpiredDate + a_pOTSLicenseInfo.GetComputerNickName();
|
|
|
+
|
|
|
+ // encrypt license string
|
|
|
+ OTSCrypt oCrypt = new OTSCrypt();
|
|
|
+ String strKey = CRYTP_KEY_STRING;
|
|
|
+
|
|
|
+ oCrypt.Encrypt(strKey, strLicense);
|
|
|
+
|
|
|
+ // return the encrypt license string
|
|
|
+
|
|
|
+ return strLicense;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get license type string
|
|
|
+ public String GetLicenseTypeIdString(OTS_LICENSE_TYPE a_nLicTypeId)
|
|
|
+ {
|
|
|
+
|
|
|
+ String strLicType = "";
|
|
|
+
|
|
|
+ if (a_nLicTypeId >= OTS_LICENSE_TYPE.MIN && a_nLicTypeId <= OTS_LICENSE_TYPE.MAX)
|
|
|
+ {
|
|
|
+ strLicType = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSETYPEID_FIRST + (int)a_nLicTypeId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return strLicType;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get software pack id string
|
|
|
+ public String GetSoftwarePackIdString(OTS_SOFT_PACKAGE_ID a_nPackId)
|
|
|
+ {
|
|
|
+ //AFX_MANAGE_STATE(AfxGetStaticModuleState());
|
|
|
+ String strPackId = "";
|
|
|
+
|
|
|
+ if (a_nPackId >= OTS_SOFT_PACKAGE_ID.MIN && a_nPackId <= OTS_SOFT_PACKAGE_ID.MAX)
|
|
|
+ {
|
|
|
+ strPackId = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_SOFTWARETYPEID_FIRST + (int)a_nPackId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return strPackId;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get license info item
|
|
|
+ public bool GetLicnseInfoItem(COTSLicenseInfo a_pOTSLicenseInfo, String a_strTitle, String a_strValue)
|
|
|
+ {
|
|
|
+
|
|
|
+ // check title, value strings
|
|
|
+ a_strTitle.Trim();
|
|
|
+ a_strValue.Trim();
|
|
|
+ if (a_strTitle == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (a_strValue == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // license info item
|
|
|
+ for (uint i = 0; i <= (int)OTS_LICENSE_INFO_ITEMS.MAX; ++i)
|
|
|
+ {
|
|
|
+ String strFileItemTitle;
|
|
|
+ strFileItemTitle = MultiLang.GetInstance().GetStringByKey( DataPublic.GrpOtherParam, DataPublic.IDS_LICENSE_INFO_TITLE_FIRST + (int)i);
|
|
|
+ if (a_strTitle.CompareTo(strFileItemTitle) == 0)
|
|
|
+ {
|
|
|
+ switch ((OTS_LICENSE_INFO_ITEMS)i)
|
|
|
+ {
|
|
|
+ case OTS_LICENSE_INFO_ITEMS.COMPUTER_NICK_NAME:
|
|
|
+ {
|
|
|
+ a_pOTSLicenseInfo.SetComputerNickName(a_strValue);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ case OTS_LICENSE_INFO_ITEMS.MACHINE_ID:
|
|
|
+ {
|
|
|
+ if (a_strValue.Length == MACHINEID_STR_LEN)
|
|
|
+ {
|
|
|
+ a_pOTSLicenseInfo.SetMachineId(a_strValue);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case OTS_LICENSE_INFO_ITEMS.SOFTWARE_PACK_ID:
|
|
|
+ {
|
|
|
+ int nPackId = 0;
|
|
|
+ if (StringToInt(a_strValue, nPackId) &&
|
|
|
+ (OTS_SOFT_PACKAGE_ID)nPackId >= OTS_SOFT_PACKAGE_ID.MIN &&
|
|
|
+ (OTS_SOFT_PACKAGE_ID)nPackId <= OTS_SOFT_PACKAGE_ID.MAX)
|
|
|
+ {
|
|
|
+ a_pOTSLicenseInfo.SetPackId((otsdataconst.OTS_SOFT_PACKAGE_ID)nPackId);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case OTS_LICENSE_INFO_ITEMS.LICENSE_TYPE:
|
|
|
+ {
|
|
|
+ int nLicType = 0;
|
|
|
+ if (StringToInt(a_strValue, (int)nLicType) &&
|
|
|
+ (OTS_LICENSE_TYPE)nLicType >= OTS_LICENSE_TYPE.MIN &&
|
|
|
+ (OTS_LICENSE_TYPE)nLicType <= OTS_LICENSE_TYPE.MAX)
|
|
|
+ {
|
|
|
+ a_pOTSLicenseInfo.SetLicType((otsdataconst.OTS_LICENSE_TYPE)nLicType);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case OTS_LICENSE_INFO_ITEMS.EXPIRE_DATE:
|
|
|
+ {
|
|
|
+ // Expire data format "%d%m%Y"
|
|
|
+ if (IsDigitString(a_strValue) && a_strValue.Length == EXPIRE_DATE_STR_LENGTH)
|
|
|
+ {
|
|
|
+ String strDay = a_strValue.PadLeft(2);
|
|
|
+ int nDay = 0;
|
|
|
+ StringToInt(strDay, nDay);
|
|
|
+ String strMonth = a_strValue.Substring(2, 2);
|
|
|
+ int nMonth = 0;
|
|
|
+ StringToInt(strMonth, nMonth);
|
|
|
+ String strYear = a_strValue.PadRight(4);
|
|
|
+ int nYear = 0;
|
|
|
+ StringToInt(strYear, nYear);
|
|
|
+ DateTime oDataTime = new DateTime();
|
|
|
+ oDataTime.AddDays(nDay);
|
|
|
+ oDataTime.AddMonths(nMonth);
|
|
|
+ oDataTime.AddYears(nYear);
|
|
|
+ a_pOTSLicenseInfo.SetExpireDate(oDataTime);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // value string is invalid , return FALSE
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // can't find matching title, return FALSE
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // load license info from a text file
|
|
|
+ public COTSLicenseInfo LoadLicenseInfoFromTextFile(String a_strFilePathName)
|
|
|
+ {
|
|
|
+ // license info
|
|
|
+ COTSLicenseInfo pOTSLicenseInfo = new COTSLicenseInfo();
|
|
|
+
|
|
|
+ // check file name
|
|
|
+ a_strFilePathName.Trim();
|
|
|
+ if (a_strFilePathName == "")
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // load string lines from the file
|
|
|
+ List<string> listLineStr = LoadTextFileToCStingList(a_strFilePathName, -1);
|
|
|
+
|
|
|
+ // process string line
|
|
|
+ bool bRet = false;
|
|
|
+ foreach (var strLine in listLineStr)
|
|
|
+ {
|
|
|
+ // split the string line with ":"
|
|
|
+ List<string> listStr = SplitString(strLine, DataPublic.FILE_TITLE_SPLIT);
|
|
|
+
|
|
|
+ // jump over the string if it is invalid
|
|
|
+ // it should have a title string and item string
|
|
|
+ if ((int)listStr.Count != TEXTFILE_ITEM_COLUMN_NUMBER)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String strTitle = listStr[0];
|
|
|
+ String strItem = listStr[1];
|
|
|
+
|
|
|
+ // get license info item
|
|
|
+ bRet = (GetLicnseInfoItem(pOTSLicenseInfo, strTitle, strItem) || bRet);
|
|
|
+ }
|
|
|
+
|
|
|
+ // check read result
|
|
|
+ if (!bRet)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // return the license info
|
|
|
+ return pOTSLicenseInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> SplitString(String a_strSource, string a_strSep)
|
|
|
+ {
|
|
|
+ // string list
|
|
|
+ List<String> listString = new List<string>();
|
|
|
+
|
|
|
+ // source string
|
|
|
+ String strSource = a_strSource;
|
|
|
+
|
|
|
+ // find the first separator
|
|
|
+ int nPosLast = 0;
|
|
|
+ var nPos = listString.Find(delegate (string str) { return str.EndsWith(a_strSource); });
|
|
|
+
|
|
|
+ // found the separator?
|
|
|
+ while (Convert.ToInt32(nPos) >= nPosLast)
|
|
|
+ {
|
|
|
+ // there is no string between two seperator if nPos == nPosLast
|
|
|
+ if (Convert.ToInt32(nPos) == nPosLast)
|
|
|
+ {
|
|
|
+ listString.Add("");
|
|
|
+ nPosLast++;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // get the string between two separator
|
|
|
+ String strValue = strSource.Substring(nPosLast, Convert.ToInt32(nPos) - nPosLast);
|
|
|
+ strValue.Trim();
|
|
|
+
|
|
|
+ // add the string into the string list
|
|
|
+ listString.Add(strValue);
|
|
|
+
|
|
|
+ nPosLast = Convert.ToInt32(nPos) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // try to find the next separator
|
|
|
+ nPos = listString.Find(delegate (string str) { return str.EndsWith(a_strSep); });
|
|
|
+ }
|
|
|
+
|
|
|
+ // push the last one into the string list
|
|
|
+ String strLastValue = strSource.PadRight(strSource.Length - nPosLast);
|
|
|
+ strLastValue.Trim();
|
|
|
+ listString.Add(strLastValue);
|
|
|
+
|
|
|
+ // return the string list
|
|
|
+ return listString;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> LoadTextFileToCStingList(String a_strPathName, int a_nLine /*= -1*/)
|
|
|
+ {
|
|
|
+ // string list
|
|
|
+ List<String> listStr = new List<string>();
|
|
|
+
|
|
|
+ // read the file
|
|
|
+ String strLine = "";
|
|
|
+
|
|
|
+ // load
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // open the file
|
|
|
+ System.Diagnostics.Process.Start(a_strPathName, strLine);
|
|
|
+
|
|
|
+ int nLine = 0;
|
|
|
+ BinaryReader reader = new BinaryReader(File.Open(strLine, FileMode.Open));
|
|
|
+ while (nLine != a_nLine)
|
|
|
+ {
|
|
|
+ // get a line
|
|
|
+
|
|
|
+ // remove comments
|
|
|
+ // int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT);
|
|
|
+ string nCommentPos = listStr.Find(delegate (string str) { return str.EndsWith("1"); });
|
|
|
+ if (Convert.ToInt32(nCommentPos) != -1)
|
|
|
+ {
|
|
|
+ // remove comments
|
|
|
+ strLine = strLine.PadLeft(Convert.ToInt32(nCommentPos));
|
|
|
+ }
|
|
|
+
|
|
|
+ // process the line
|
|
|
+ strLine.Trim();
|
|
|
+
|
|
|
+ // jump over empty lines
|
|
|
+ if (strLine == "")
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ listStr.Add(strLine);
|
|
|
+ }
|
|
|
+
|
|
|
+ File.Create(strLine).Close();
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+
|
|
|
+ System.IO.File.Delete(strLine);
|
|
|
+ return listStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ // return string list
|
|
|
+ return listStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ // create license info from a text file
|
|
|
+ public bool CreateLicenseInfoFileText(COTSLicenseInfo a_pOTSLicenseInfo)
|
|
|
+ {
|
|
|
+ string a_strPathName = "";
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ a_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(a_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(a_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // get license info text body
|
|
|
+ String strLicenseInfoTextBody = GetLicenseInfoTextBody(a_pOTSLicenseInfo);
|
|
|
+ File.WriteAllText(strLicenseInfoTextBody, "");
|
|
|
+
|
|
|
+ File.Delete(strLicenseInfoTextBody);
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // create license file for the software package pointed by the license info
|
|
|
+ public bool CreateLicenseFile(COTSLicenseInfo a_pOTSLicenseInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ // get software package system data folder name
|
|
|
+ String strOTSPackSysDataPathName = GetOTSPackSysDataPathName(GetPackId());
|
|
|
+
|
|
|
+ // check if the folder name is right
|
|
|
+ if (strOTSPackSysDataPathName == "")
|
|
|
+ {
|
|
|
+ // failed to get software package system data folder name
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if the path exist
|
|
|
+ if (!Exists(strOTSPackSysDataPathName))
|
|
|
+ {
|
|
|
+ // create the folder
|
|
|
+ if (!CreateFolder(strOTSPackSysDataPathName))
|
|
|
+ {
|
|
|
+ // failed to create software package system data folder
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // get software package license file pathname
|
|
|
+ String strFilePathName = GetPackLicenseFilePathName(GetPackId());
|
|
|
+ strFilePathName.Trim();
|
|
|
+ if (strFilePathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return CreateLicenseFile(strFilePathName, a_pOTSLicenseInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ private OTS_SOFT_PACKAGE_ID GetPackId()
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ // create license file (a license key)
|
|
|
+ public bool CreateLicenseFile(String a_strFilePathName, COTSLicenseInfo a_pOTSLicenseInfo)
|
|
|
+ {
|
|
|
+ // check if the file pathname is empty
|
|
|
+ a_strFilePathName.Trim();
|
|
|
+ if (a_strFilePathName == "")
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // create a license file pointer
|
|
|
+ COTSLicenseFile pOTSLicenseFilePtr = new COTSLicenseFile();
|
|
|
+ pOTSLicenseFilePtr.SetLicenseInfo(a_pOTSLicenseInfo);
|
|
|
+
|
|
|
+ // return save license file result
|
|
|
+ return Save(a_strFilePathName);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public class COTSLicInfoFile
|
|
|
+ {
|
|
|
+
|
|
|
+ // modify flag
|
|
|
+ bool m_bModify;
|
|
|
+
|
|
|
+ string m_strPathName = "";
|
|
|
+
|
|
|
+ // license info list
|
|
|
+ List<COTSLicenseInfo> m_listLicenseInfo;
|
|
|
+
|
|
|
// constructor
|
|
|
-
|
|
|
+ public COTSLicInfoFile()
|
|
|
+ {
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+
|
|
|
+ // initialization
|
|
|
+ public void Init()
|
|
|
+ {
|
|
|
+ m_listLicenseInfo.Clear();
|
|
|
+ m_bModify = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OTSLicInfoFile(COTSLicInfoFile a_oSource)
|
|
|
+ {
|
|
|
+ // can't copy itself
|
|
|
+ if (a_oSource == this)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Duplicate(a_oSource);
|
|
|
+ }
|
|
|
+
|
|
|
+ // duplication
|
|
|
+ public void Duplicate(COTSLicInfoFile a_oSource)
|
|
|
+ {
|
|
|
+ // initialization
|
|
|
+ Init();
|
|
|
+
|
|
|
+ // copy data over
|
|
|
+ foreach (var pLicenseInfoSource in a_oSource.m_listLicenseInfo)
|
|
|
+ {
|
|
|
+ COTSLicenseInfo pLicenseInfoNew = new COTSLicenseInfo(pLicenseInfoSource);
|
|
|
+ m_listLicenseInfo.Add(pLicenseInfoNew);
|
|
|
+ }
|
|
|
+ m_strPathName = a_oSource.m_strPathName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ public String GetPathName() { return m_strPathName; }
|
|
|
+
|
|
|
+ public void SetPathName(String a_strPathName) { m_strPathName = a_strPathName; }
|
|
|
+
|
|
|
+ // modify flag
|
|
|
+ public bool IsModified() { return m_bModify; }
|
|
|
+
|
|
|
+ public void SetModify(bool a_bModify = true) { m_bModify = a_bModify; }
|
|
|
+
|
|
|
+ // license info list
|
|
|
+ public List<COTSLicenseInfo> GetLicenseInfoList() { return m_listLicenseInfo; }
|
|
|
+
|
|
|
+ public bool Load(String a_strPathName)
|
|
|
+ {
|
|
|
+
|
|
|
+ // check file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ a_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+ // open the particle analysis standard file
|
|
|
+
|
|
|
+ if (!File.Exists(a_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(a_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Save()
|
|
|
+ {
|
|
|
+ string m_strPathName = "";
|
|
|
+ // check if file pathname is empty
|
|
|
+ m_strPathName.Trim();
|
|
|
+ if (m_strPathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ m_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(m_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(m_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public class COTSLicenseFile
|
|
|
+ {
|
|
|
+
|
|
|
+ string m_strPathName = "";
|
|
|
+
|
|
|
+ // license info
|
|
|
+ COTSLicenseInfo m_pLicenseInfo;
|
|
|
+
|
|
|
+ public COTSLicenseFile()
|
|
|
+ {
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+ // initialization
|
|
|
+ public void Init()
|
|
|
+ {
|
|
|
+ m_strPathName = "";
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OTSLicenseFile(COTSLicenseFile a_oSource)
|
|
|
+ {
|
|
|
+ // can't copy itself
|
|
|
+ if (a_oSource == this)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // copy data over
|
|
|
+ Duplicate(a_oSource);
|
|
|
+ }
|
|
|
+
|
|
|
+ // duplication
|
|
|
+ public void Duplicate(COTSLicenseFile a_oSource)
|
|
|
+ {
|
|
|
+ // initialization
|
|
|
+ Init();
|
|
|
+
|
|
|
+ // copy data over
|
|
|
+ m_pLicenseInfo = new COTSLicenseInfo((a_oSource.m_pLicenseInfo));
|
|
|
+ m_strPathName = a_oSource.m_strPathName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ public String GetPathName() { return m_strPathName; }
|
|
|
+
|
|
|
+ public COTSLicenseInfo GetLicenseInfo() { return m_pLicenseInfo; }
|
|
|
+
|
|
|
+ public void SetLicenseInfo(COTSLicenseInfo a_pLicenseInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ m_pLicenseInfo = new COTSLicenseInfo(a_pLicenseInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public bool Load(String a_strPathName)
|
|
|
+ {
|
|
|
+
|
|
|
+ // check file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ a_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+ // open the particle analysis standard file
|
|
|
+
|
|
|
+ if (!File.Exists(a_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(a_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Save(String a_strPathName)
|
|
|
+ {
|
|
|
+ // check the file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get path of the file
|
|
|
+ String strFilePath = GetFolderName(a_strPathName);
|
|
|
+ if (strFilePath != "")
|
|
|
+ {
|
|
|
+ // check if file folder exists
|
|
|
+ if (!Exists(strFilePath))
|
|
|
+ {
|
|
|
+ // create the file folder
|
|
|
+ if (!CreateFolder(strFilePath))
|
|
|
+ {
|
|
|
+ // failed to create the file folder
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // check file pathname
|
|
|
+ a_strPathName.Trim();
|
|
|
+ if (a_strPathName == "")
|
|
|
+ {
|
|
|
+ // file open dialog
|
|
|
+ OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
+ if (openFileDialog.ShowDialog() != DialogResult.OK)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // get file pathname
|
|
|
+
|
|
|
+ a_strPathName = openFileDialog.FileName;
|
|
|
+ }
|
|
|
+ // open the particle analysis standard file
|
|
|
+
|
|
|
+ if (!File.Exists(a_strPathName))
|
|
|
+ {
|
|
|
+ // failed to open the file
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileStream NewText = File.Create(a_strPathName);
|
|
|
+ NewText.Close();
|
|
|
+
|
|
|
+
|
|
|
+ // file pathname
|
|
|
+ m_strPathName = a_strPathName;
|
|
|
+
|
|
|
+ // ok, return TRUE
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String GetFolderName(String a_strPathName)
|
|
|
+ {
|
|
|
+ Char[] sPath = new char[ DataPublic.MAX_PATH];
|
|
|
+
|
|
|
+ String.Format(sPath.ToString(), DataPublic.MAX_PATH.ToString(), a_strPathName);
|
|
|
+
|
|
|
+ // PathRemoveFileSpec(sPath);
|
|
|
+
|
|
|
+ // 1、首先判断文件或者文件路径是否存在
|
|
|
+ if (File.Exists(sPath.ToString()))
|
|
|
+ {
|
|
|
+ // 2、根据路径字符串判断是文件还是文件夹
|
|
|
+ FileAttributes attr = File.GetAttributes(sPath.ToString());
|
|
|
+ // 3、根据具体类型进行删除
|
|
|
+ if (attr == FileAttributes.Directory)
|
|
|
+ {
|
|
|
+ // 3.1、删除文件夹
|
|
|
+ Directory.Delete(sPath.ToString(), true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 3.2、删除文件
|
|
|
+ File.Delete(sPath.ToString());
|
|
|
+ }
|
|
|
+ File.Delete(sPath.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ return sPath.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Exists(string a_sPath)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool CreateFolder(string a_strFolder)
|
|
|
+ {
|
|
|
+ // make sure the folder name string are not empty
|
|
|
+ String strFolder = a_strFolder;
|
|
|
+ strFolder.Trim();
|
|
|
+ if (strFolder == "")
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // if the folder exist?
|
|
|
+ if (Exists(strFolder))
|
|
|
+ {
|
|
|
+ // is a real folder?
|
|
|
+ if (IsFolder(strFolder))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // create folder
|
|
|
+
|
|
|
+ // remove back slash if there
|
|
|
+ String strParentFolder = strFolder;
|
|
|
+ strParentFolder.Trim();
|
|
|
+
|
|
|
+ // find last back slash position
|
|
|
+ int nBackSlashPos = Convert.ToInt32(strParentFolder);
|
|
|
+ if (nBackSlashPos > 0)
|
|
|
+ {
|
|
|
+ // get the folder name
|
|
|
+ String strFolderName = strParentFolder.PadRight(strParentFolder.Length - nBackSlashPos - 1);
|
|
|
+
|
|
|
+ // separate the folder string
|
|
|
+ strParentFolder = strParentFolder.PadLeft(nBackSlashPos);
|
|
|
+ if (!IsValidFileName(strFolderName))
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // try to create folder from the base folder
|
|
|
+ if (!CreateFolder(strParentFolder))
|
|
|
+ {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // create the folder
|
|
|
+ bool bRet = true;
|
|
|
+
|
|
|
+ // return folder create result
|
|
|
+ return bRet;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool IsFolder(string a_strFolder)
|
|
|
+ {
|
|
|
+ if (a_strFolder != "")
|
|
|
+ {
|
|
|
+ return HasAttribute(a_strFolder, DataPublic.FILE_ATTRIBUTE_DIRECTORY);
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if the given string is valid file name or not
|
|
|
+ public bool IsValidFileName(string a_sFileName)
|
|
|
+ {
|
|
|
+ String strFileName = a_sFileName;
|
|
|
+ const String INVALIDFILENAMECHAR = "\\/:*?\"<>";
|
|
|
+ a_sFileName = INVALIDFILENAMECHAR;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool HasAttribute(string a_strFolder, int a_nAttribute)
|
|
|
+ {
|
|
|
+ int flags = Convert.ToInt32(a_strFolder);
|
|
|
+ return (flags != DataPublic.INVALID_FILE_ATTRIBUTES) && (flags == a_nAttribute);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|