/// <summary> /// DTTypeの値を、対応するType型の値に変換する /// </summary> /// <param name="dtType">DTTypeの値</param> /// <returns>変換後のType型の値</returns> private Type ConvertDTTypeToType(DTType dtType) { Type retType; // 変換後のType型の値 // DTTypeは列挙型なので、switch文を使用する switch (dtType) { case DTType.Boolean: retType = typeof(bool); break; case DTType.ByteArray: retType = typeof(byte[]); break; case DTType.Char: retType = typeof(char); break; case DTType.DateTime: retType = typeof(DateTime); break; case DTType.Decimal: retType = typeof(decimal); break; case DTType.Double: retType = typeof(double); break; case DTType.Int16: retType = typeof(short); break; case DTType.Int32: retType = typeof(int); break; case DTType.Int64: retType = typeof(long); break; case DTType.Single: retType = typeof(float); break; case DTType.String: retType = typeof(string); break; default: // 通常はここに来ることはない retType = typeof(string); break; } // 変換後のType型の値を返す return(retType); }
/// <summary> /// System.Data.DataTableをDTTableに変換する /// </summary> /// <param name="table">変換元のSystem.Data.DataTable</param> /// <returns>変換後のDTTable</returns> public static DTTable FromDataTable(DataTable table) { // テーブル定義 DTTable dt = new DTTable(table.TableName); // 列定義 foreach (DataColumn col in table.Columns) { DTType type = ConvertTypeToDTType(col.DataType); dt.Cols.Add(new DTColumn(col.ColumnName, type)); } // 値追加(削除された行以外) DTRow dr; foreach (DataRow row in table.Rows) { // 行をテーブルに追加 dr = dt.Rows.AddNew(); // 各列ごとに値を追加 foreach (DataColumn col in table.Columns) { if (row.RowState != System.Data.DataRowState.Deleted) { dr[col.ColumnName] = row[col.ColumnName]; } else { // 行が削除されている場合は、元の値を取得する dr[col.ColumnName] = row[col.ColumnName, DataRowVersion.Original]; } } // 行ステータスを復元 if (row.RowState == System.Data.DataRowState.Detached) { dr.RowState = DataRowState.Detached; } else if (row.RowState == System.Data.DataRowState.Added) { dr.RowState = DataRowState.Added; } else if (row.RowState == System.Data.DataRowState.Modified) { dr.RowState = DataRowState.Modified; } else if (row.RowState == System.Data.DataRowState.Deleted) { dr.RowState = DataRowState.Deleted; } else { dr.RowState = DataRowState.Unchanged; } } // DTTableを返す return(dt); }
/// <summary>キャストする</summary> /// <param name="dtType">指定の型</param> /// <param name="o">指定の値</param> /// <returns>変換後の値</returns> public static object AutoCast(DTType dtType, object o) { switch (dtType) { case DTType.Boolean: return(Convert.ToBoolean(o)); case DTType.ByteArray: // バイト配列の自動変換はサポートしない throw new Exception( "It is a data type which is not supporting automatic conversion (System.Byte[]). "); case DTType.Char: return(Convert.ToChar(o)); case DTType.DateTime: return(Convert.ToDateTime(o)); case DTType.Decimal: return(Convert.ToDecimal(o)); case DTType.Double: return(Convert.ToDouble(o)); case DTType.Int16: return(Convert.ToInt16(o)); case DTType.Int32: return(Convert.ToInt32(o)); case DTType.Int64: return(Convert.ToInt64(o)); case DTType.Single: return(Convert.ToSingle(o)); case DTType.String: // 文字列の自動変換はサポートしない throw new Exception( "It is a data type which is not supporting automatic conversion (System.String). "); default: throw new Exception( "it is a data type which is not supported. "); } }
/// <summary>列挙型を電文上の文字列に変換する</summary> /// <param name="dtType">列挙型</param> /// <returns>電文上の文字列</returns> public static string EnumToString(DTType dtType) { switch (dtType) { case DTType.Boolean: return("Boolean"); case DTType.ByteArray: return("ByteArray"); case DTType.Char: return("Char"); case DTType.DateTime: return("DateTime"); case DTType.Decimal: return("Decimal"); case DTType.Double: return("Double"); case DTType.Int16: return("Int16"); case DTType.Int32: return("Int32"); case DTType.Int64: return("Int64"); case DTType.Single: return("Single"); case DTType.String: return("String"); default: throw new Exception( "it is a data type which is not supported. "); } }
///// <summary>コンストラクタ</summary> //public DTColumn() { } // ⇒ 列名、列型は必須 /// <summary>コンストラクタ</summary> /// <param name="colName">列名</param> /// <param name="colType">列型</param> public DTColumn(string colName, DTType colType) { // 列名の文字制限(正規表現チェックだが、 // コンバートのことを考え棟梁部品は使用しない。) Regex rgx = new Regex("^[a-zA-Z0-9_-]+$"); Match mch = rgx.Match(colName); if (mch.Success) { this._colName = colName; this._colType = colType; } else { // 列名が不正 throw new Exception( "A column name is inaccurate. " + " The regular expression of the character which can be used:" + " \"^[a-zA-Z0-9_-]+$\""); } }
/// <summary> /// To converts all string data to premitive Type by colType and cellString. /// </summary> /// <param name="colType">Column data type</param> /// <param name="cellString">Cell data</param> /// <returns>return Premitive type object</returns> public static object PrimitivetypeFromString(DTType colType, string cellString) { object convertedPrimitiveType = null; if (cellString != null) { // ByteArray if (colType == DTType.ByteArray) { byte[] cellByte = Convert.FromBase64String(cellString); convertedPrimitiveType = cellByte; } // DateTime else if (colType == DTType.DateTime) { string ymd = cellString.Split('-')[0]; string hmsf = cellString.Split('-')[1]; DateTime cellDttm = new DateTime( int.Parse(ymd.Split('/')[0]), int.Parse(ymd.Split('/')[1]), int.Parse(ymd.Split('/')[2]), int.Parse(hmsf.Split(':')[0]), int.Parse(hmsf.Split(':')[1]), int.Parse(hmsf.Split(':')[2].Split('.')[0]), int.Parse(hmsf.Split(':')[2].Split('.')[1])); convertedPrimitiveType = cellDttm; } else { convertedPrimitiveType = DTColumn.AutoCast(colType, cellString.ToString()); } } return(convertedPrimitiveType); }
/// <summary>タイプをチェックする</summary> /// <param name="o">指定の値</param> /// <param name="dtType">指定の型</param> /// <returns>true・false</returns> /// <remarks>値をセルに設定するときに使用する。</remarks> public static bool CheckType(object o, DTType dtType) { switch (o.GetType().ToString()) { case "System.Boolean": if (dtType == DTType.Boolean) { return(true); } else { return(false); } case "System.Byte[]": if (dtType == DTType.ByteArray) { return(true); } else { return(false); } case "System.Char": if (dtType == DTType.Char) { return(true); } else { return(false); } case "System.DateTime": if (dtType == DTType.DateTime) { return(true); } else { return(false); } case "System.Decimal": if (dtType == DTType.Decimal) { return(true); } else { return(false); } case "System.Double": if (dtType == DTType.Double) { return(true); } else { return(false); } case "System.Int16": if (dtType == DTType.Int16) { return(true); } else { return(false); } case "System.Int32": if (dtType == DTType.Int32) { return(true); } else { return(false); } case "System.Int64": if (dtType == DTType.Int64) { return(true); } else { return(false); } case "System.Single": if (dtType == DTType.Single) { return(true); } else { return(false); } case "System.String": if (dtType == DTType.String) { return(true); } else { return(false); } default: throw new Exception( "it is a data type which is not supported. "); } }
/// <summary> /// DTTypeの値を、対応するType型の値に変換する /// </summary> /// <param name="dtType">DTTypeの値</param> /// <returns>変換後のType型の値</returns> private Type ConvertDTTypeToType(DTType dtType) { Type retType; // 変換後のType型の値 // DTTypeは列挙型なので、switch文を使用する switch (dtType) { case DTType.Boolean: retType = typeof(bool); break; case DTType.ByteArray: retType = typeof(byte[]); break; case DTType.Char: retType = typeof(char); break; case DTType.DateTime: retType = typeof(DateTime); break; case DTType.Decimal: retType = typeof(decimal); break; case DTType.Double: retType = typeof(double); break; case DTType.Int16: retType = typeof(short); break; case DTType.Int32: retType = typeof(int); break; case DTType.Int64: retType = typeof(long); break; case DTType.Single: retType = typeof(float); break; case DTType.String: retType = typeof(string); break; default: // 通常はここに来ることはない retType = typeof(string); break; } // 変換後のType型の値を返す return retType; }
/// <summary> /// To converts all string data to premitive Type by colType and cellString. /// </summary> /// <param name="colType">Column data type</param> /// <param name="cellString">Cell data</param> /// <returns>return Premitive type object</returns> public static object PrimitivetypeFromString(DTType colType, string cellString) { object convertedPrimitiveType = null; if (cellString != null) { // ByteArray if (colType == DTType.ByteArray) { byte[] cellByte = Convert.FromBase64String(cellString); convertedPrimitiveType = cellByte; } // DateTime else if (colType == DTType.DateTime) { string ymd = cellString.Split('-')[0]; string hmsf = cellString.Split('-')[1]; DateTime cellDttm = new DateTime( int.Parse(ymd.Split('/')[0]), int.Parse(ymd.Split('/')[1]), int.Parse(ymd.Split('/')[2]), int.Parse(hmsf.Split(':')[0]), int.Parse(hmsf.Split(':')[1]), int.Parse(hmsf.Split(':')[2].Split('.')[0]), int.Parse(hmsf.Split(':')[2].Split('.')[1])); convertedPrimitiveType = cellDttm; } else { convertedPrimitiveType = DTColumn.AutoCast(colType, cellString.ToString()); } } return convertedPrimitiveType; }
/// <summary> /// Read informations from Windows registry /// </summary> public static void ReadRegistry() { var paths = new List <string> { "SOFTWARE\\Disc Soft", "SOFTWARE\\Wow6432Node\\Disc Soft", "SOFTWARE\\DT Soft", "SOFTWARE\\Wow6432Node\\DT Soft" }; var versions = new List <string> { "DAEMON Tools Pro", "DAEMON Tools Ultra" }; RegistryKey regKey = null; foreach (string v in versions) { foreach (string p in paths) { regKey = Registry.LocalMachine.OpenSubKey(p + "\\" + v); if (regKey != null) { break; } } if (regKey != null) { break; } } if (regKey == null) { _path = ""; _type = DTType.None; _version = ""; } else { _path = (string)regKey.GetValue("Path"); if (!_path.EndsWith("\\")) { _path += "\\"; } if (_path.IndexOf("Lite", StringComparison.Ordinal) > 0) { _type = DTType.Lite; _path += "DTLite.exe"; } else { _type = _path.IndexOf("Ultra", StringComparison.Ordinal) > 0 ? DTType.Ultra : DTType.Pro; _path += "DTAgent.exe"; } _version = (string)regKey.GetValue("Version Major") + '.' + (string)regKey.GetValue("Version Minor") + '.' + (string)regKey.GetValue("Version Release"); } }
/// <summary>列挙型を電文上の文字列に変換する</summary> /// <param name="dtType">列挙型</param> /// <returns>電文上の文字列</returns> public static string EnumToString(DTType dtType) { switch (dtType) { case DTType.Boolean: return "Boolean"; case DTType.ByteArray: return "ByteArray"; case DTType.Char: return "Char"; case DTType.DateTime: return "DateTime"; case DTType.Decimal: return "Decimal"; case DTType.Double: return "Double"; case DTType.Int16: return "Int16"; case DTType.Int32: return "Int32"; case DTType.Int64: return "Int64"; case DTType.Single: return "Single"; case DTType.String: return "String"; default: throw new Exception( "it is a data type which is not supported. "); } }
/// <summary>キャストする</summary> /// <param name="dtType">指定の型</param> /// <param name="o">指定の値</param> /// <returns>変換後の値</returns> public static object AutoCast(DTType dtType, object o) { switch (dtType) { case DTType.Boolean: return Convert.ToBoolean(o); case DTType.ByteArray: // バイト配列の自動変換はサポートしない throw new Exception( "It is a data type which is not supporting automatic conversion (System.Byte[]). "); case DTType.Char: return Convert.ToChar(o); case DTType.DateTime: return Convert.ToDateTime(o); case DTType.Decimal: return Convert.ToDecimal(o); case DTType.Double: return Convert.ToDouble(o); case DTType.Int16: return Convert.ToInt16(o); case DTType.Int32: return Convert.ToInt32(o); case DTType.Int64: return Convert.ToInt64(o); case DTType.Single: return Convert.ToSingle(o); case DTType.String: // 文字列の自動変換はサポートしない throw new Exception( "It is a data type which is not supporting automatic conversion (System.String). "); default: throw new Exception( "it is a data type which is not supported. "); } }
/// <summary>タイプをチェックする</summary> /// <param name="o">指定の値</param> /// <param name="dtType">指定の型</param> /// <returns>true・false</returns> /// <remarks>値をセルに設定するときに使用する。</remarks> public static bool CheckType(object o, DTType dtType) { switch (o.GetType().ToString()) { case "System.Boolean": if (dtType == DTType.Boolean) { return true; } else { return false; } case "System.Byte[]": if (dtType == DTType.ByteArray) { return true; } else { return false; } case "System.Char": if (dtType == DTType.Char) { return true; } else { return false; } case "System.DateTime": if (dtType == DTType.DateTime) { return true; } else { return false; } case "System.Decimal": if (dtType == DTType.Decimal) { return true; } else { return false; } case "System.Double": if (dtType == DTType.Double) { return true; } else { return false; } case "System.Int16": if (dtType == DTType.Int16) { return true; } else { return false; } case "System.Int32": if (dtType == DTType.Int32) { return true; } else { return false; } case "System.Int64": if (dtType == DTType.Int64) { return true; } else { return false; } case "System.Single": if (dtType == DTType.Single) { return true; } else { return false; } case "System.String": if (dtType == DTType.String) { return true; } else { return false; } default: throw new Exception( "it is a data type which is not supported. "); } }
/// <summary> /// Read informations from Windows registry /// </summary> public static void ReadRegistry() { var paths = new List<string> { "SOFTWARE\\Disc Soft", "SOFTWARE\\Wow6432Node\\Disc Soft", "SOFTWARE\\DT Soft", "SOFTWARE\\Wow6432Node\\DT Soft" }; var versions = new List<string> { "DAEMON Tools Pro", "DAEMON Tools Ultra" }; RegistryKey regKey = null; foreach (string v in versions) { foreach (string p in paths) { regKey = Registry.LocalMachine.OpenSubKey(p + "\\" + v); if (regKey != null) break; } if (regKey != null) break; } if (regKey == null) { _path = ""; _type = DTType.None; _version = ""; } else { _path = (string)regKey.GetValue("Path"); if (!_path.EndsWith("\\")) { _path += "\\"; } if (_path.IndexOf("Lite", StringComparison.Ordinal) > 0) { _type = DTType.Lite; _path += "DTLite.exe"; } else { _type = _path.IndexOf("Ultra", StringComparison.Ordinal) > 0 ? DTType.Ultra : DTType.Pro; _path += "DTAgent.exe"; } _version = (string)regKey.GetValue("Version Major") + '.' + (string)regKey.GetValue("Version Minor") + '.' + (string)regKey.GetValue("Version Release"); } }