public Log(DirectoryPathIds DirectoryPathId = DirectoryPathIds.CurrentDirectory1, string LogFileName = null) { if (DirectoryPathId == DirectoryPathIds.CurrentDirectory1) { DirectoryPath = Directory.GetCurrentDirectory(); } else if (DirectoryPathId == DirectoryPathIds.CurrentDirectory2) { DirectoryPath = Environment.CurrentDirectory; } else if (DirectoryPathId == DirectoryPathIds.Desktop) { DirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } else if (DirectoryPathId == DirectoryPathIds.AppDataFolder) { LocalAppDataFolder localAppDataFolder = new LocalAppDataFolder(); DirectoryPath = localAppDataFolder.AssemblyFolderPath; } if (LogFileName == null) { LogFileName = "LOG" + GetTimeStamp().ToString(); } LogFullFileName = LogFileName + ".txt"; FullFilePath = DirectoryPath + @"\" + LogFullFileName; try { if (File.Exists(FullFilePath)) { LogFileName += "(" + FileCount++.ToString() + ")"; LogFullFileName = LogFileName + ".txt"; FullFilePath = DirectoryPath + @"\" + LogFullFileName; } using (FileStream fs = File.Create(FullFilePath)) { Console.WriteLine("file created. Name: " + LogFullFileName); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public void Save() { JsonSerializer serializer = new JsonSerializer(); serializer.Formatting = Formatting.Indented; var configFile = this; LocalAppDataFolder localAppDataFolder = new LocalAppDataFolder();//an object with a method that return a stream to the AppData/local project folder where I save the config file FieldInfo fieldInfo = this.GetType().GetField("ConfigsFileName", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); string value = (string)fieldInfo.GetValue(null); using (FileStream stream = localAppDataFolder.GetFile(value)) using (StreamWriter sw = new StreamWriter(stream)) using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, configFile); } }
public static T GetConfigs <T>() where T : ConfigsTools, new() { T result; JsonSerializer serializer = new JsonSerializer(); FieldInfo fieldInfo = typeof(T).GetField("ConfigsFileName", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); string value = (string)fieldInfo.GetValue(null); LocalAppDataFolder localAppDataFolder = new LocalAppDataFolder(); using (FileStream stream = localAppDataFolder.GetFile(value)) using (StreamReader sw = new StreamReader(stream)) using (JsonReader reader = new JsonTextReader(sw)) { result = serializer.Deserialize <T>(reader); } if (result == null) { result = new T(); } return(result); }