Explorar el Código

添加control中的类方法

sunyi hace 5 años
padre
commit
de1a47ac1f
Se han modificado 1 ficheros con 641 adiciones y 0 borrados
  1. 641 0
      OTS/OTSModelSharp/OTSFileSys.cs

+ 641 - 0
OTS/OTSModelSharp/OTSFileSys.cs

@@ -0,0 +1,641 @@
+using Microsoft.Office.Interop.Word;
+using OTSDataType;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml;
+
+namespace OTSModelSharp
+{
+    class OTSFileSys
+    {
+        public static NLog.Logger Nloger = NLog.LogManager.GetCurrentClassLogger();
+
+        public string FILE_ATTRIBUTE_DIRECTORY = "0x00000010";
+
+        public string FILE_ATTRIBUTE_READONLY = "0x00000001";
+
+        public  const String STR_COMPANYNAME = "Config";
+
+        public  const String STR_SYSTEM_DATA = "SysData";
+
+        public  const String STR_LOG = "Log";
+
+        public  const String STR_APPNAME_OTSINCA = "OTSIncA";
+
+        public const String STR_PROG_DATA = "ProData";
+
+        public const String STR_MEASURE_PREFERENCE_FILE_NAME = "OTSProgMgrParam.pmf";
+
+        public const String STR_REPORT_PREFERENCE_FILE_NAME = "OTSReportMgrParam.rpf";
+
+        //SySSTDData.db
+        public const String SYS_STD_LIB_FILE_NAME = "SySSTDData.db"; //GeneralSTDData
+
+        // check if the file exists or not
+        public bool Exists(string a_sPath)
+        {
+            return File.Exists(a_sPath) == true;
+        }
+
+        // check if the given string is valid file name or not
+         public  bool IsValidFileName(string a_sFileName)
+        {
+            String strFileName = a_sFileName;
+            const String INVALIDFILENAMECHAR="\\/:*?\"<>";
+
+            return strFileName.IndexOf(INVALIDFILENAMECHAR) == -1;
+        }
+
+        // copy a file
+      public  bool CopyAFile(string a_strSourceFile, string a_strTargetFile,  bool a_bOverwrite /*= FALSE*/)
+	  {
+       
+
+		// make sure the source file exist
+		if (!Exists(a_strSourceFile))
+		{
+                Nloger.Error("CopyAFile: source file doesn't exist");
+                return false;
+		}
+
+		// quit if the target file exists and can't be overwritten
+		if (!a_bOverwrite && Exists(a_strTargetFile))
+		{
+                Nloger.Error("CopyAFile: target file exists and can't be overwritten" );
+                return false;
+		}
+
+            // copy file
+            
+            System.IO.File.Copy(a_strSourceFile, a_strTargetFile, !a_bOverwrite);
+          
+      //      if (AllSelected(System.IO.File.Copy(a_strSourceFile, a_strTargetFile, !a_bOverwrite)))
+		    //{
+      //          Nloger.Error("CopyAFile: copy(%s) file to %s failed.error : %s", a_strSourceFile, a_strTargetFile);
+      //          return false;
+		    //}
+
+           
+
+            // ok, return TRUE
+            return true;
+	  }
+
+       
+
+        // move a file
+        public bool MoveAFile(string a_strSourceFile, string a_strTargetFile,  bool a_bOverwrite /*= TRUE*/)
+	    {
+       
+
+		// make sure the source file exist
+		if (!Exists(a_strSourceFile))
+		{
+                Nloger.Error("MoveAFile: source file doesn't exist");
+                return false;
+		}
+
+		// the target exists?
+		if (Exists(a_strTargetFile))
+		{
+			// quit if the target file can't be overwritten
+			if(!a_bOverwrite)
+			{
+                    Nloger.Error("MoveAFile: target file exists and can't be overwritten");
+                    return false;
+			}
+
+          System.IO.File.Delete(a_strTargetFile);
+
+            //if (DeleteAFile(a_strTargetFile))
+            //{
+            //                 Nloger.Error("MoveAFile: can't delete the exist file %s", a_strTargetFile);
+            //                 return false;
+            //}
+        }
+
+           System.IO.File.Move(a_strSourceFile, a_strTargetFile);
+        //if (System.IO.File.Move(a_strSourceFile, a_strTargetFile))
+        //{
+        //              Nloger.Error("MoveAFile: move(%s) file to %s failed.error : %s", a_strSourceFile, a_strTargetFile);
+        //              return false;
+        //}
+
+            // ok, return TRUE
+            return true;
+	    }
+
+        // delete a file.
+        public bool DeleteAFile(string a_strFileName)
+        {
+            
+
+            // return TRUE if the file is not exist
+            if (!Exists(a_strFileName))
+            {
+                Nloger.Error("DeleteAFile: %s file does not exist.", a_strFileName);
+                return true;
+            }
+
+            // quit if the file is read-only
+            FileInfo fInfo = new FileInfo(a_strFileName);
+
+            if (fInfo.IsReadOnly)
+            {
+                Nloger.Error("DeleteAFile: %s file is readonly.", a_strFileName);
+                return false;
+            }
+
+            System.IO.File.Delete(a_strFileName);
+            //if (!::DeleteFile(a_strFileName))
+            //{
+            //    Nloger.Error("DeleteAFile: delete (%s) file failed. error: %s", a_strFileName);
+            //}
+
+            // ok, return TRUE
+            return true;
+        }
+
+        // delete all files in the folder
+        public  bool DeleteAllFiles(string a_strFolder, string a_sFilter /*= nullptr*/)
+        {
+            
+
+            // return TRUE if the folder is not exist
+            if (!Exists(a_strFolder))
+            {
+                Nloger.Error("DeleteAllFiles: %s folder does not exist.", a_strFolder);
+                return true;
+            }
+
+            // make sure this is a folder
+            if (!IsFolder(a_strFolder))
+            {
+                Nloger.Error("DeleteAllFiles: %s is not a folder.", a_strFolder);
+                return false;
+            }
+
+            // is the folder read-only?
+            if (IsReadOnly(a_strFolder))
+            {
+                Nloger.Error("DeleteAllFiles: %s folder is readonly.", a_strFolder);
+                return false;
+            }
+
+            //// get all files in the folder
+            //Font searchFile = new Font();
+            //String filePattern=a_strFolder;
+            //if (a_sFilter!="")
+            //{
+            //    filePattern += "\\";
+            //    filePattern += a_sFilter;
+            //}
+            //else
+            //{
+            //    filePattern +="\\*.*";
+            //}
+            //bool ret = searchFile.
+            //    //.FindFile(filePattern);
+
+            //// delete all files in the folder
+            //while (ret)
+            //{
+
+            //    // get a file from the folder
+            //    ret = searchFile.
+
+            //    String filePath = searchFile.GetFilePath();
+
+            //    // make sure that this is a real file, jump over if is not 
+            //    if (searchFile.IsDots() || searchFile.IsDirectory())
+            //    {
+            //        continue;
+            //    }
+
+            //    // delete the file
+            //    if (!DeleteAFile(searchFile.GetFilePath()))
+            //    {
+            //        Nloger.Error("DeleteAllFiles: delete (%s) file failed.", searchFile.GetFilePath());
+            //        return false;
+            //    }
+            //}
+
+            //searchFile.Close();
+            GetDirectories();
+            // ok, return TRUE
+            return true;
+        }
+
+        // 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=="")
+            {
+                Nloger.Error( "CreateFolder: invalid folder name is empty.");
+                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 = strParentFolder.LastIndexOf('\\');
+            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))
+                {
+                    Nloger.Error( "CreateFolder: invalid folder or file name %s.", strFolderName);
+                    return false;
+                }
+
+                // try to create folder from the base folder 
+                if (!CreateFolder(strParentFolder))
+                {
+                    Nloger.Error("CreateFolder: failed to create %s folder.", strParentFolder);
+                    return false;
+                }
+            }
+
+            // create the folder
+            // bool bRet = CreateDirectory(strFolder, null) == true;
+             Directory.CreateDirectory(strFolder);
+            // return folder create result
+            return true;
+        }
+
+        // check if this is an existing folder
+       public  bool IsFolder(string a_strFolder)
+        {
+            if (Directory.Exists(a_strFolder))
+            {
+                XmlDocument doc = new XmlDocument();
+                XmlElement root = doc.DocumentElement;
+                return root.HasAttribute(a_strFolder, FILE_ATTRIBUTE_DIRECTORY);
+            }
+
+            return false;
+        }
+
+        // deletes a folder and contents recursively.
+       public bool DeleteFolder(string a_strFolder)
+        {
+            // make sure the folder name string are not empty
+            String strFolder = a_strFolder;
+            strFolder.Trim();
+            if (strFolder=="")
+            {
+                Nloger.Error("CreateFolder: invalid folder name is empty.");
+                return false;
+            }
+
+            // quit if the folder is not exist
+            if (!Exists(strFolder))
+            {
+                Nloger.Error("DeleteFolder: %s folder is not exist.", strFolder);
+                return true;
+            }
+
+            // make sure it is a folder
+            if (!IsFolder(strFolder))
+            {
+                Nloger.Error("DeleteFolder: %s is a folder.", strFolder);
+                return false;
+            }
+
+            // readonly?
+            if (IsReadOnly(strFolder))
+            {
+                Nloger.Error("DeleteFolder: %s folder is readonly.", strFolder);
+                return false;
+            }
+
+            // remove every thing in the folder
+            //CFileFind fileSearch;
+            //String filePattern = strFolder ;
+            //filePattern += "\\*.*";
+            //bool ret = fileSearch.FindFile(filePattern);
+            //while (ret)
+            //{
+            //    // get a file or a folder
+            //    ret = fileSearch.FindNextFile();
+
+            //    // jump over if this is a dots item
+            //    if (fileSearch.IsDots())
+            //    {
+            //        continue;
+            //    }
+
+            //    // is a sub folder
+            //    if (fileSearch.IsDirectory())
+            //    {
+            //        // delete the sub folder
+            //        if (!DeleteFolder(fileSearch.GetFilePath()))
+            //        {
+            //            Nloger.Error("DeleteFolder: delete %s sub folder failed.", fileSearch.GetFilePath());
+            //            return false;
+            //        }
+            //    }
+            //    // or a file, delete the file
+            //    else if (!DeleteAFile(fileSearch.GetFilePath()))
+            //    {
+            //        Nloger.Error("DeleteFolder: delete %s file failed.", fileSearch.GetFilePath());
+            //        return false;
+            //    }
+            //}
+            GetDirectories();
+            Directory.GetFiles(strFolder);
+            Directory.Delete(a_strFolder);
+            // delete the folderstrFolder
+            //if (!Directory.Delete(a_strFolder))
+            //{
+            //    Nloger.Error("DeleteFolder: remove directory %s failed.", strFolder);
+            //    return false;
+            //}
+            // ok return TRUE
+            return true;
+        }
+
+        public void GetDirectories()
+        {
+            string a_strFolder = @"c:\\temp\\";
+            //如果该路径不存在,就创建该路径。
+            if (!Directory.Exists(a_strFolder))
+                Directory.CreateDirectory(a_strFolder);
+            //获取该路径下一层的所有路径
+            string[] dirs = Directory.GetDirectories(a_strFolder);
+            foreach (string dir in dirs)
+            {
+                //获取路径信息
+                DirectoryInfo dirIf = new DirectoryInfo(dir);
+                string name = dirIf.Name;
+                string fullName = dirIf.FullName;
+                //获取该路径下一层的所有路径信息
+                DirectoryInfo[] subDirs = dirIf.GetDirectories();
+                foreach (DirectoryInfo subDir in subDirs)
+                {
+                    string sub_name = subDir.Name;
+                }
+            }
+            //获取文件信息,path为文件路径,包括文件名称
+            FileInfo fileInfo = new FileInfo(a_strFolder);
+            string extension = fileInfo.Extension;//扩展名
+            string moveToFileName = "移动文件的目标位置,包含文件名";
+            File.Move(fileInfo.FullName, moveToFileName);
+            //获取文件夹下所有的文件
+            DirectoryInfo dicInfo = new DirectoryInfo(a_strFolder);
+            FileInfo[] fileInfos = dicInfo.GetFiles();
+            foreach (FileInfo file in fileInfos.OrderBy(f => int.Parse(f.Name)))
+            {
+                //文件信息;
+            }
+        }
+
+        // check if the file or folder is read-only
+        public bool IsReadOnly(string a_strPathName)
+        {           
+            XmlDocument doc = new XmlDocument();
+            XmlElement root = doc.DocumentElement;
+            return root.HasAttribute(a_strPathName, FILE_ATTRIBUTE_DIRECTORY);
+        }
+
+        // sets the read-only flag for a file or a folder.
+       public bool SetReadOnly(string a_strPathName, bool a_bReadOnly /*=TRUE*/)
+        {
+            uint flags = Convert.ToUInt32(a_strPathName);
+            if (a_bReadOnly)
+            {
+                flags |= Convert.ToUInt32(FILE_ATTRIBUTE_READONLY);
+            }
+            else
+            {
+                flags &= Convert.ToUInt32(FILE_ATTRIBUTE_READONLY);
+            }
+
+             File.SetAttributes(a_strPathName, FileAttributes.ReadOnly);
+
+            return true;
+            
+        }
+
+        // get system common data folder pathname
+        // return "" if failed
+       public String GetOSCommonDataPathName()
+        {
+
+            String strPathName="";
+            if (strPathName.PadRight(1) != ".\\")
+            {
+                strPathName += ".\\";
+            }
+            return strPathName;
+        }
+
+        // get company system data path
+       public String GetCompanySysDataPathName()
+        {
+            // get common data pathname string
+            String strCommonDataPathName = GetOSCommonDataPathName();
+            if (strCommonDataPathName=="")
+            {
+                // failed to get common data pathname string
+                Nloger.Error("GetOTSPackSysDataPathName: failed to common data pathname string.");
+                return "";
+            }
+
+            // company system data pathname
+            // e.g. "c:\ProgramData\Config\"
+            String strCmpSysDataPath = strCommonDataPathName + STR_COMPANYNAME + "\\" + STR_SYSTEM_DATA + "\\";
+
+            // return company system data pathname 
+            return strCmpSysDataPath;
+        }
+
+
+        // get company log pathname
+       public String GetCompanyLogPathName()
+        {
+            // get common data pathname string
+            String strCommonDataPathName = GetOSCommonDataPathName();
+            if (strCommonDataPathName=="")
+            {
+                // failed to get company system data folder string
+                Nloger.Error("GetCompayLogPathName: failed to common data pathname string.");
+                return "";
+            }
+
+            // software package log path
+            // e.g. "c:\ProgramData\Log\"
+            String strCompanyLogPathName = strCommonDataPathName + STR_COMPANYNAME + "\\" + STR_LOG + "\\";
+
+            // return software package log path
+            return strCompanyLogPathName;
+        }
+
+        // get software pack system data path
+        public String GetOTSPackSysDataPathName(otsdataconst.OTS_SOFT_PACKAGE_ID a_nPackId)
+        {
+            // get app package name
+            String strAppPackageName="";
+            switch (a_nPackId)
+            {
+                case otsdataconst.OTS_SOFT_PACKAGE_ID.OTSIncA:
+                    strAppPackageName = STR_APPNAME_OTSINCA;
+                    break;
+
+                case otsdataconst.
+OTS_SOFT_PACKAGE_ID.OTSPartA:
+                    strAppPackageName = STR_APPNAME_OTSINCA;
+                    break;
+
+                default:
+                    Nloger.Error("GetOTSPackSysDataPathName: invalid software package id.");
+                    return "";
+            }
+
+            // get common data pathname string
+            String strCommonDataPathName = GetOSCommonDataPathName();
+            if (strCommonDataPathName=="")
+            {
+                // can't common data path
+                Nloger.Error("GetOTSPackSysDataPathName: failed to common data path.");
+                return "";
+            }
+
+            // software package system data pathname
+            // e.g. "c:\ProgramData\Config\SysData\"
+            //CString strOTSSysDataPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + strAppPackageName + _T("\\") + STR_SYSTEM_DATA + _T("\\");
+            String strOTSSysDataPathName = strCommonDataPathName + STR_COMPANYNAME + "\\" + STR_SYSTEM_DATA + "\\";
+
+            // return software package system data path
+            return strOTSSysDataPathName;
+        }
+
+        // get software pack program data path
+        public String GetOTSPackProgDataPathName(otsdataconst.OTS_SOFT_PACKAGE_ID a_nPackId)
+        {
+            // get app package name
+            String strAppPackageName ="";
+            switch (a_nPackId)
+            {
+                case otsdataconst.OTS_SOFT_PACKAGE_ID.OTSIncA:
+                    strAppPackageName = STR_APPNAME_OTSINCA;
+                    break;
+
+                case otsdataconst.OTS_SOFT_PACKAGE_ID.OTSPartA:
+                    strAppPackageName = STR_APPNAME_OTSINCA;
+                    break;
+
+                default:
+                    Nloger.Error("GetOTSPackProgDataPathName: invalid software package id.");
+                    return "";
+            }
+
+            // get common data pathname string
+            String strCommonDataPathName = GetOSCommonDataPathName();
+            if (strCommonDataPathName=="")
+            {
+                // can't common data path
+                Nloger.Error("GetOTSPackProgDataPathName: failed to common data path.");
+                return "";
+            }
+
+            // software package program data pathname
+            // e.g. "c:\ProgramData\Config\ProData\"
+            //CString strOTSProDataPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + strAppPackageName + _T("\\") + STR_PROG_DATA + _T("\\");
+            String strOTSProDataPathName = strCommonDataPathName + STR_COMPANYNAME + "\\" + STR_PROG_DATA + "\\";
+
+            // return software package program data path
+            return strOTSProDataPathName;
+        }
+
+        // get software pack preference file path name
+       public String GetOTSPackMeasurePrefFilePathName(otsdataconst.OTS_SOFT_PACKAGE_ID a_nPackId)
+        {
+            // get software package system data pathname
+            String strOTSPackSysDataPathName = GetOTSPackSysDataPathName(a_nPackId);
+
+            // check if software package system data pathname is right
+            if (strOTSPackSysDataPathName=="")
+            {
+                // failed to get software package system data pathname
+                Nloger.Error("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string.");
+                return "";
+            }
+
+            // software package project manager file pathname
+            // i.e. "c:\ProgramData\Config\SysData\OTSProgMgrParam.pmf"
+            String strOTSPackProgMgrPathName = strOTSPackSysDataPathName + STR_MEASURE_PREFERENCE_FILE_NAME;
+
+            // return software package license file pathname 
+            return strOTSPackProgMgrPathName;
+        }
+
+        // get software pack preference file path name
+       public String GetOTSPackReportPrefFilePathName(otsdataconst.OTS_SOFT_PACKAGE_ID a_nPackId)
+        {
+            // get software package system data pathname
+            String strOTSPackSysDataPathName = GetOTSPackSysDataPathName(a_nPackId);
+
+            // check if software package system data pathname is right
+            if (strOTSPackSysDataPathName=="")
+            {
+                // failed to get software package system data pathname
+                Nloger.Error("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string.");
+                return "";
+            }
+
+            // software package project manager file pathname
+            // i.e. "c:\ProgramData\Config\SysData\OTSReportMgrParam.rp"
+            String strOTSPackProgMgrPathName = strOTSPackSysDataPathName + STR_REPORT_PREFERENCE_FILE_NAME;
+
+            // return software package license file pathname 
+            return strOTSPackProgMgrPathName;
+        }
+
+      public  String GetOTSPackSysSTDLibFilePathName(otsdataconst.OTS_SOFT_PACKAGE_ID a_nPackId)
+        {
+            // get software package system data pathname
+            String strOTSPackSysDataPathName = GetOTSPackSysDataPathName(a_nPackId);
+
+            // check if software package system data pathname is right
+            if (strOTSPackSysDataPathName=="")
+            {
+                // failed to get software package system data pathname
+                Nloger.Error("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string.");
+                return "";
+            }
+
+            // software package project manager file pathname
+            // i.e. "c:\ProgramData\Config\SysData\SySSTDData.db"
+            String strOTSPackSysSTDlibPathName = strOTSPackSysDataPathName + SYS_STD_LIB_FILE_NAME;
+
+            // return software package license file pathname 
+            return strOTSPackSysSTDlibPathName;
+        }
+
+    }
+}