Beispiel #1
0
    public static int TransformDataToByteArray(object objData, ref byte[] byteData)
    {
        int iStt = XErrorCode.ST_SUCCESS;

        try
        {
            InxAPI.API2.AIPDefectData clsMergeDefectData = objData as InxAPI.API2.AIPDefectData;
            if (clsMergeDefectData != null)
            {
                BinaryFormatter clsBinary = new BinaryFormatter();
                MemoryStream    clsStream = new MemoryStream();
                clsBinary.Serialize(clsStream, clsMergeDefectData);
                byteData    = new byte[clsStream.Length];
                byteData    = clsStream.ToArray();
                g_tTempData = new Byte[byteData.Length];
                byteData.CopyTo(g_tTempData, 0);
            }
        }
        catch (Exception ex)
        {
            Logger.Bomb.Error("[TransformDataToByteArray] : " + ex);
            XStatus.Report(XStatus.Type.Procedure, MethodBase.GetCurrentMethod(), XStatus.GetExceptionLine(ex));
        }
        return(iStt);
    }
Beispiel #2
0
    /// <summary>反序列化 Xml 文件中的資料轉成物件資料。。</summary>
    /// <typeparam name="T">型別,可能為自訂的 Class or 其他基本型別。</typeparam>
    /// <param name="strPath">檔案路徑。</param>
    /// <param name="clsData">物件資料。</param>
    /// <returns>執行結果狀態碼 XErrorCode 。</returns>
    public static int Deserialize <T>(string strPath, ref T clsData)
    {
        int    iStt        = XErrorCode.ST_SUCCESS;
        object clsTempData = null;
        //FileStream clsFileStream = null;
        MemoryStream  clsMemoryStream  = null;
        XmlSerializer clsXmlSerializer = null;
        XmlReader     clsXmlReader     = null;
        UTF8Encoding  clsEncoding      = null;

        try
        {
            clsEncoding = new UTF8Encoding();
            //using (clsFileStream = new FileStream(strPath, FileMode.Open))
            using (clsMemoryStream = new MemoryStream(clsEncoding.GetBytes(File.ReadAllText(strPath))))
            {
                clsXmlSerializer = new XmlSerializer(typeof(T));
                //clsXmlReader = XmlReader.Create(clsFileStream);
                //clsTempData = (T)clsXmlSerializer.Deserialize(clsXmlReader);
                clsTempData = (T)clsXmlSerializer.Deserialize(clsMemoryStream);
                if (clsTempData != null)
                {
                    clsData = (T)clsTempData;
                }
                else
                {
                    clsData = default(T);
                }
                clsMemoryStream.Close();
                //clsXmlReader.Close();
                //clsFileStream.Close();
            }
        }
        catch (Exception ex)
        {
            Logger.Bomb.Error("[Deserialize<T>] - " + ex.ToString());
            if (clsXmlReader != null)
            {
                clsXmlReader.Close();
            }
            //if (clsFileStream != null) clsFileStream.Close();
            if (clsMemoryStream != null)
            {
                clsMemoryStream = null;
            }
            iStt = XErrorCode.ER_SYSTEM;
            XStatus.Report(XStatus.Type.Windows, MethodInfo.GetCurrentMethod(), XStatus.GetExceptionLine(ex));
        }
        finally
        {
            //clsFileStream = null;
            clsXmlSerializer = null;
            clsXmlReader     = null;
            clsMemoryStream  = null;
        }
        return(iStt);
    }
Beispiel #3
0
 /// <summary>直接顯示到一個 TextBox 內。</summary>
 /// <param name="txt">TextBox 元件。</param>
 public static void Display(TextBox txt)
 {
     try
     {
         InxGeneral.XUiTool.TextBoxSetText(txt, LogMessage[0] + "\r\n" + txt.Text);
     }
     catch (Exception ex)
     {
         XStatus.Report(XStatus.Type.Vision, MethodInfo.GetCurrentMethod(), XStatus.GetExceptionLine(ex));
     }
 }
Beispiel #4
0
    /// <summary>序列化資料轉成 Xml 格式。</summary>
    /// <typeparam name="T">型別,可能為自訂的 Class or 其他基本型別。</typeparam>
    /// <param name="strPath">檔案路徑。</param>
    /// <param name="clsData">物件資料。</param>
    /// <returns>執行結果狀態碼 XErrorCode 。</returns>
    public static int Serialize <T>(string strPath, T clsData)
    {
        int iStt = XErrorCode.ST_SUCCESS;
        XmlWriterSettings       clsXmlSettings             = null;
        XmlWriter               clsXmlWriter               = null;
        XmlSerializer           clsXmlSerializer           = null;
        XmlSerializerNamespaces clsXmlSerializerNamespaces = null;

        try
        {
            clsXmlSettings = new XmlWriterSettings();
            clsXmlSettings.NewLineHandling = NewLineHandling.None;
            clsXmlSettings.Indent          = true;

            using (clsXmlWriter = XmlWriter.Create(strPath, clsXmlSettings))
            {
                clsXmlSerializer           = new XmlSerializer(clsData.GetType());
                clsXmlSerializerNamespaces = new XmlSerializerNamespaces();
                clsXmlSerializerNamespaces.Add(string.Empty, string.Empty);
                clsXmlSerializer.Serialize(clsXmlWriter, clsData, clsXmlSerializerNamespaces);
                clsXmlWriter.Close();
            }
        }
        catch (Exception ex)
        {
            Logger.Bomb.Error("[Serialize<T>] - " + ex.ToString());
            if (clsXmlWriter != null)
            {
                clsXmlWriter.Close();
            }
            iStt = XErrorCode.ER_SYSTEM;
            XStatus.Report(XStatus.Type.Windows, MethodInfo.GetCurrentMethod(), XStatus.GetExceptionLine(ex));
        }
        finally
        {
            clsXmlSettings             = null;
            clsXmlWriter               = null;
            clsXmlSerializer           = null;
            clsXmlSerializerNamespaces = null;
        }
        return(iStt);
    }
Beispiel #5
0
    /// <summary>指定資料夾的位置,去抓取底下有多少個資料夾,並且獲取該名稱。</summary>
    /// <param name="strFolder">資料夾路徑。</param>
    /// <returns>回傳目前指定的資料夾底下有多少個資料夾。</returns>
    public static List <string> GetFoldersNameFromFolder(string strFolder)
    {
        int           iStt           = XErrorCode.ST_SUCCESS;
        string        strParentName  = string.Empty;
        List <string> strFolderLists = new List <string>();

        try
        {
            // 確保資料夾是否存在
            if (!Directory.Exists(strFolder))
            {
                return(strFolderLists);
            }

            // 設定要讀取的資料夾
            DirectoryInfo   clsTarget            = new DirectoryInfo(strFolder);
            DirectoryInfo[] clsTargetFolderLists = clsTarget.GetDirectories();
            if (clsTargetFolderLists.Length > 0)
            {
                foreach (DirectoryInfo clsFolder in clsTargetFolderLists)
                {
                    iStt = GetFileNameFromFolder(clsFolder.FullName);
                    if (iStt == XErrorCode.ST_SUCCESS)
                    {
                        strFolderLists.Add(clsFolder.FullName);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Bomb.Error("[GetFoldersNameFromFolder] - " + ex.ToString());
            XStatus.Report(XStatus.Type.Windows, MethodInfo.GetCurrentMethod(), XStatus.GetExceptionLine(ex));
        }
        return(strFolderLists);
    }