/// <summary> /// Gets a count of setting tags that contain the string portion specified. /// </summary> /// <param name="partial"></param> /// <returns></returns> public static int GetCountByPartial(string partial) { GetSingleton(); lock (m_singleton) { string inputString; string[] inputFields; int count = 0; string UserSettingsFileName; UserSettingsFileName = GetAppPath(); if (File.Exists(UserSettingsFileName)) { // load the records into memory // open the file for reading try { StreamReader fileReader; string SettingsName; string SettingsValue; fileReader = new StreamReader(UserSettingsFileName); while ((inputString = fileReader.ReadLine()) != null) { if (!inputString.Contains(",")) { continue; } inputFields = inputString.Split(','); SettingsName = inputFields[0]; SettingsValue = inputFields[1]; if (SettingsName.Contains(partial)) { count++; } } // end while fileReader.Close(); } catch (Exception ex) { FileLogging.Set("GetCountByPartial exception : " + ex.Message); return(0); } // end try } // end if file exists else // the files has not been created, so this is an error { // MessageBox.Show("could not open user.config file"); return(0); } return(count); } }
public static void WriteToLog(string error, LOG_TYPE type) { error = DateTime.UtcNow.ToString() + ": " + error; Console.WriteLine(error); FileLogging.Set(error); }
public static void Trace(Exception ex) { List <string> lines = new List <string>(); string time = DateTime.UtcNow.ToString() + " UTC:"; FileLogging.Set(time + " ex: " + ex.Message + " stack trace: " + Environment.NewLine); FileLogging.Set(ex.StackTrace + Environment.NewLine); }
//System.Reflection.MethodBase.GetCurrentMethod() public void Trace(Exception ex, LOG_TYPE type) { List <string> lines = new List <string>(); string time = DateTime.UtcNow.ToString() + " UTC:"; Console.WriteLine(time + " ex: " + ex.Message + " stack trace: " + Environment.NewLine); Console.WriteLine(ex.StackTrace + Environment.NewLine); FileLogging.Set(time + " ex: " + ex.Message + " stack trace: " + Environment.NewLine); FileLogging.Set(ex.StackTrace + Environment.NewLine); }
public void Log(string error, LOG_TYPE type) { lock (singleton) { if (!ScreenLoggerShowing && m_ScreenLogger != null) { ScreenLoggerShowing = true; m_ScreenLogger.ShowForm(); } Console.WriteLine(error); if (m_ScreenLogger != null) { m_ScreenLogger.Log(error); } FileLogging.Set(error); } }
public static string GetGPSCommPort() { string commPortString = null; try { // Query the device list trough the WMI. If you want to get // all the properties listen in the MSDN article mentioned // below, use "select * from Win32_PnPEntity" instead! ManagementObjectSearcher deviceList = new ManagementObjectSearcher("Select Name, Status from Win32_PnPEntity"); /// USB to UART Bridge Controller (COM6) //string filename = "C:\\Users\\David\\Pictures\\test\\devlist.txt"; //StreamWriter sw = File.AppendText(filename); // Any results? There should be! if (deviceList != null) { // Enumerate the devices foreach (ManagementObject device in deviceList.Get()) { // To make the example more simple, string name = device.GetPropertyValue("Name").ToString(); string status = device.GetPropertyValue("Status").ToString(); if (name.Contains("USB to UART Bridge Controller") || name.Contains("USB-to-Serial")) { // get the comm number try { int index = name.LastIndexOf("COM"); string ss = name.Substring(index); string[] sa = ss.Split(')'); // int commPortNum = Convert.ToInt32(ss[1]); commPortString = sa[0]; return(commPortString); } catch { return(null); } } // sw.WriteLine(name + "\r\n"); // Uncomment these lines and use the "select * query" if you // want a VERY verbose list // foreach (PropertyData prop in device.Properties) // Console.WriteLine( "\t" + prop.Name + ": " + prop.Value); // More details on the valid properties: // http://msdn.microsoft.com/en-us/library/aa394353(VS.85).aspx // Console.WriteLine("Device name: {0}", name); // Console.WriteLine("\tStatus: {0}", status); // Part II, Evaluate the device status. // bool working = ((status == "OK") || (status == "Degraded") // || (status == "Pred Fail")); // Console.WriteLine("\tWorking?: {0}", working); } } } catch (Exception ex) { FileLogging.Set("GetGPSCommPort ex: " + ex.Message); } return(null); }
public static void Set(string setting, string value) { GetSingleton(); lock (m_singleton) { string inputString; string[] inputFields; inputFields = new string[2]; bool settingDone = false; string UserSettingsFileName; UserSettingsFileName = GetAppPath(); // does file exist already ? try { if (File.Exists(UserSettingsFileName)) { // load the records into memory // open the file for reading StreamReader fileReader; StreamWriter fileWriter; string NewSettingsName; string NewSettingsValue; // COUNT THE ENTRIES fileReader = new StreamReader(UserSettingsFileName); int i = 0; while ((inputString = fileReader.ReadLine()) != null) { if (inputString.Contains(",")) { i++; } } if (i == 0) // error { // MessageBox.Show("file user.config exists but is empty"); } // ALLOCATE MEMORY int numLines = i; // MessageBox.Show("numLines = "+ numLines.ToString()); fileReader.Close(); // LOAD THE LINES fileReader = new StreamReader(UserSettingsFileName); i = 0; string[] SettingsName; string[] SettingsValue; SettingsName = new string[numLines]; SettingsValue = new string[numLines]; while ((inputString = fileReader.ReadLine()) != null) { if (!inputString.Contains(",")) { break; } inputFields = inputString.Split(','); SettingsName[i] = inputFields[0]; SettingsValue[i] = inputFields[1]; if (SettingsName[i] == setting) { SettingsValue[i] = value; settingDone = true; } i++; } // end while // now we have either changed an existing setting or will add a new one, // write all settings to the file fileReader.Close(); // delete the old file and write the new one File.Delete(UserSettingsFileName); fileWriter = new StreamWriter(UserSettingsFileName); for (i = 0; i < numLines; i++) { fileWriter.WriteLine(SettingsName[i] + "," + SettingsValue[i]); } if (!settingDone) // we did not find an existing setting to change, add a new one { NewSettingsName = setting; NewSettingsValue = value; fileWriter.WriteLine(NewSettingsName + "," + NewSettingsValue); } fileWriter.Close(); } // end if file exists else // the files has not been created, so creat it and store the settings { StreamWriter fileWriter; string NewSettingsName; string NewSettingsValue; NewSettingsName = setting; NewSettingsValue = value; fileWriter = new StreamWriter(UserSettingsFileName); fileWriter.WriteLine(NewSettingsName + "," + NewSettingsValue); fileWriter.Close(); } }// end try catch (ArgumentException) { FileLogging.Set("ArgumentException.. " + UserSettingsFileName); } // end catch catch (IOException) { FileLogging.Set("IOException.. " + UserSettingsFileName); } // end catch catch (UnauthorizedAccessException) { FileLogging.Set("UnauthorizedAccessException.. " + UserSettingsFileName); } // end catch } } // end Set
public static string Get(string setting) { GetSingleton(); lock (m_singleton) { string inputString; string[] inputFields; bool readingDone = false; string value = null; string UserSettingsFileName; UserSettingsFileName = GetAppPath(); // does file exits already ? if (File.Exists(UserSettingsFileName)) { // load the records into memory // open the file for reading try { StreamReader fileReader; string SettingsName; string SettingsValue; fileReader = new StreamReader(UserSettingsFileName); while ((inputString = fileReader.ReadLine()) != null) { if (!inputString.Contains(",")) { break; } inputFields = inputString.Split(','); SettingsName = inputFields[0]; SettingsValue = inputFields[1]; if (SettingsName == setting) { value = SettingsValue; readingDone = true; break; } } // end while if (!readingDone) // we did not find an existing setting to change, add a new one { // MessageBox.Show("did not find string -" + setting + "- in file"); fileReader.Close(); return(null); } fileReader.Close(); } catch (Exception ex) { FileLogging.Set("Get exception : " + ex.Message); return(null); } } // end if file exists else // the files has not been created, so this is an error { // MessageBox.Show("could not open user.config file"); return(null); } return(value); } } // end get