/// <summary> /// Set value associated to the given key /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Set(string key, string value) { try { string[] allKeys = Configuration.AppSettings.Settings.AllKeys; StringUtil.ToUpper(allKeys); Array.Sort(allKeys); if (Array.BinarySearch <string>(allKeys, key.ToUpper()) < 0) { // key not found, create new entry Configuration.AppSettings.Settings.Add(key, value); } else { // key found, update existing entry Configuration.AppSettings.Settings[key].Value = value; } } catch (Exception ex) { DebuggerTool.AddData(ex, "key", key); DebuggerTool.AddData(ex, "value", value); DebuggerTool.AddData(ex, "config.FilePath", Configuration.FilePath); throw; } }
/// <summary> /// Save the key and value pair to config file. /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void Save(string key, string value) { try { Assembly assembly = Assembly.GetCallingAssembly(); string appConfig = assembly.Location; Configuration config = ConfigurationManager.OpenExeConfiguration(appConfig); string[] names = ConfigurationManager.AppSettings.AllKeys; NameValueCollection appSettings = ConfigurationManager.AppSettings; for (int i = 0; i < appSettings.Count; i++) { Console.WriteLine("#{0} Name: {1} Value: {2}", i, names[i], appSettings[i]); } config.Save(); } catch (Exception ex) { DebuggerTool.AddData(ex, "key", key); DebuggerTool.AddData(ex, "value", value); throw; } }
/// <summary> /// Write a byte array to filename /// </summary> /// <param name="filename"></param> /// <param name="content"></param> public static void WriteFile(string filename, byte[] content) { int bufSize = 1024; try { using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read, bufSize)) using (BinaryWriter w = new BinaryWriter(fs)) { for (int i = 0; i < content.Length; i++) { w.Write(content[i]); } w.Flush(); w.Close(); } } catch (Exception ex) { DebuggerTool.AddData(ex, "filename", filename); DebuggerTool.AddData(ex, "content", content); throw; } }
private static void AddData(SanityCheckException ex, params object[] data) { if (data == null) { return; } for (int i = 0; i < data.Length; i++) { DebuggerTool.AddData(ex, "idx" + i, data[i]); } }
/// <summary> /// Save the configuration file /// </summary> public void Save() { try { Configuration.Save(ConfigurationSaveMode.Modified); } catch (Exception ex) { DebuggerTool.AddData(ex, "config.FilePath", Configuration.FilePath); throw; } }
/// <summary> /// Retrieve a web page using GET method /// </summary> /// <param name="url"></param> /// <returns></returns> public static string[] GetURL(string url) { try { WebRequest request = HttpWebRequest.Create(new Uri(url)); WebResponse response = request.GetResponse(); return(FileUtil.ReadStream(response.GetResponseStream())); } catch (Exception ex) { DebuggerTool.AddData(ex, "url", url); throw; } }
/// <summary> /// Get value associated to the given key /// </summary> /// <param name="key"></param> /// <returns></returns> public string Get(string key) { try { return(Configuration.AppSettings.Settings[key].Value); } catch (Exception ex) { DebuggerTool.AddData(ex, "key", key); DebuggerTool.AddData(ex, "AllKeys", string.Concat(Configuration.AppSettings.Settings.AllKeys)); DebuggerTool.AddData(ex, "config.FilePath", Configuration.FilePath); throw; } }
/// <summary> /// Get the ConnectionString associated to the given key /// </summary> /// <param name="key"></param> /// <returns></returns> public ConnectionStringSettings GetConnectionString(string key) { try { ConnectionStringSettings results = Configuration.ConnectionStrings.ConnectionStrings[key]; return(results); } catch (Exception ex) { DebuggerTool.AddData(ex, "key", key); DebuggerTool.AddData(ex, "config.FilePath", Configuration.FilePath); throw; } }
/// <summary> /// Convert an image to a byte array /// </summary> /// <param name="imageToConvert"></param> /// <param name="formatOfImage"></param> /// <returns></returns> public static byte[] ConvertImageToByteArray(Image imageToConvert, ImageFormat formatOfImage) { try { using (MemoryStream ms = new MemoryStream()) { imageToConvert.Save(ms, formatOfImage); return(ms.ToArray()); } } catch (Exception ex) { DebuggerTool.AddData(ex, "imageToConvert", DebuggerTool.Dump(imageToConvert)); DebuggerTool.AddData(ex, "formatOfImage", DebuggerTool.Dump(formatOfImage)); throw; } }
/// <summary> /// Write a string List array to a unique temporarily filename with the specify extension. /// </summary> /// <param name="extension"></param> /// <param name="list"></param> /// <returns></returns> public static string CreateTempFile(string extension, params string[] list) { try { string filename = GenerateUniqueTempFilename(extension); tempFiles.AddFile(filename, false); WriteFile(filename, list); return(filename); } catch (Exception ex) { DebuggerTool.AddData(ex, "extension", extension); DebuggerTool.AddData(ex, "list", DebuggerTool.Dump(list)); throw; } }
/// <summary> /// get the value associated with key /// </summary> /// <param name="key"></param> /// <returns></returns> public static string Value(string key) { try { if (ConfigurationManager.AppSettings == null) { throw new ArgumentNullException("AppSettings"); } if (String.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); } string s = String.Empty; try { s = ConfigurationManager.AppSettings[key].ToString(); } catch (Exception ex) { Debug.Print(ex.ToString()); // absorb error! } if (String.IsNullOrEmpty(s)) { throw new ArgumentNullException("key=" + key); } if (s.Contains("{TEMP_FOLDER}")) { s = s.Replace("{TEMP_FOLDER}", Path.GetTempPath()); } return(s); } catch (Exception ex) { DebuggerTool.AddData(ex, "key", key); throw; } }
/// <summary> /// Create DataRelation using the DataSet /// </summary> /// <param name="ds"></param> public void SetRelation(DataSet ds) { try { if (ds == null) { throw new ArgumentNullException("ds"); } DataTable dtParent = ds.Tables[this.ParentTable]; DataTable dtChild = ds.Tables[this.ChildTable]; if (dtParent == null) { throw new ArgumentNullException("dtParent"); } if (dtChild == null) { throw new ArgumentNullException("dtChild"); } DataColumn dcParent = dtParent.Columns[this.ParentColumn]; DataColumn dcChild = dtChild.Columns[this.ChildColumn]; if (dcParent == null) { throw new ArgumentNullException("dcParent"); } if (dcChild == null) { throw new ArgumentNullException("dcChild"); } DataRelation dr = new DataRelation(RelationName, dcParent, dcChild); dr.Nested = this.Nested; ds.Relations.Add(dr); } catch (Exception ex) { DebuggerTool.AddData(ex, "ds", DebuggerTool.Dump(ds)); throw; } }
/// <summary> /// Retrieve a web page using POST method /// </summary> /// <param name="url"></param> /// <param name="contentType"></param> /// <param name="parameters"></param> /// <returns></returns> public static string PostURL(string url, string contentType, string parameters) { try { WebRequest request = HttpWebRequest.Create(new Uri(url)); request.Method = "POST"; request.ContentType = contentType; request.ContentLength = parameters.Length; FileUtil.WriteStream(request.GetRequestStream(), parameters); WebResponse response = request.GetResponse(); return(StringUtil.StringArrayToString(FileUtil.ReadStream(response.GetResponseStream()), Environment.NewLine)); } catch (Exception ex) { DebuggerTool.AddData(ex, "url", url); DebuggerTool.AddData(ex, "parameters", parameters); throw; } }
/// <summary> /// Copy a file to a different filename, with cleaning null characters. /// </summary> /// <param name="filename"></param> /// <param name="outfile"></param> public static void CleanNull(string filename, string outfile) { try { if (File.Exists(outfile)) { File.Delete(outfile); } using (StreamReader sr = new StreamReader(filename)) using (BinaryReader br = new BinaryReader(sr.BaseStream)) using (StreamWriter sw = new StreamWriter(outfile)) using (BinaryWriter bw = new BinaryWriter(sw.BaseStream)) { while (br.PeekChar() != -1) { byte b = br.ReadByte(); // skip NULL character if (b == 0) { continue; } bw.Write(b); } sw.Flush(); sw.Close(); sr.Close(); } } catch (Exception ex) { DebuggerTool.AddData(ex, "filename", filename); DebuggerTool.AddData(ex, "outfile", outfile); throw; } }
/// <summary> /// Create DataRelation to given DataSet /// </summary> /// <param name="ds"></param> /// <param name="relations"></param> public static void SetRelation(DataSet ds, params DataRelationHelper[] relations) { try { if (ds == null) { return; } for (int i = 0; i < relations.Length; i++) { DataRelationHelper r = relations[i]; r.SetRelation(ds); } } catch (Exception ex) { DebuggerTool.AddData(ex, "ds", DebuggerTool.Dump(ds)); DebuggerTool.AddData(ex, "relations", DebuggerTool.Dump(relations)); throw; } }
/// <summary> /// Write a string array to a file. /// </summary> /// <param name="filename"></param> /// <param name="content"></param> public static void WriteFile(string filename, string[] content) { try { using (StreamWriter w = new StreamWriter(filename, false)) { for (int i = 0; i < content.Length; i++) { w.WriteLine(content[i]); } w.Flush(); w.Close(); } } catch (Exception ex) { DebuggerTool.AddData(ex, "filename", filename); DebuggerTool.AddData(ex, "content", content); throw; } }
/// <summary> /// convert given object to a different object type /// </summary> /// <param name="value"></param> /// <param name="defaultValue"></param> /// <param name="conversionType"></param> /// <returns></returns> public static object ChangeType(object value, object defaultValue, Type conversionType) { if (IsNull(value)) { return(defaultValue); } try { object result = null; // special case especially for DateTime.... if (conversionType.ToString() == "System.DateTime") { try { result = DateTime.Parse(value.ToString()); return(result); } catch //(Exception ex) { // too bad, try next conversion... //DebuggerTool.AddData(ex,"value", value); //Log.LogError(ex, "ChangType, 1. DateTime.Parse()"); //Log.LogError("DateTime.Parse('" + value + "') : " + ex.Message, "ChangeType"); } } try { #if !SILVERLIGHT result = Convert.ChangeType(value, conversionType); #else result = Convert.ChangeType(value, conversionType, null); #endif return(result); } catch //(Exception ex) { // too bad, try next conversion... //DebuggerTool.AddData(ex,"value", value); //Log.LogError(ex, "ChangeType, 2. Convert.ChangeType()"); //Log.LogError("Convert.ChangeType('" + value + "') : " + ex.Message, "ChangeType"); } try { // invoke the Parse method: type.Parse(value, NumberStyles.Any); object[] arguments = new object[] { value.ToString(), NumberStyles.Any }; result = conversionType.InvokeMember("Parse", System.Reflection.BindingFlags.InvokeMethod, null, result, arguments); return(result); } catch //(Exception ex) { // too bad, try next conversion... //DebuggerTool.AddData(ex,"value", value); //Log.LogError(ex, "ChangeType, 3. Invoking Parse method"); //Log.LogError("<Object>.Parse('" + value + "', NumberStyle.Any) : " + ex.Message, "ChangeType"); } //Log.LogError("No more conversion handle, returning default value", "ChangeType", // string.Format("value={0} default={1} type={2}", value, defaultValue, conversionType)); return(defaultValue); } catch (Exception ex) { DebuggerTool.AddData(ex, "value", value); DebuggerTool.AddData(ex, "defaultValue", defaultValue); DebuggerTool.AddData(ex, "conversionType", conversionType); throw; } }