public virtual void Clean() { if (_streamWriter == null || String.IsNullOrEmpty(FilePath)) { return; } ChoFile.Clean(FilePath); }
public ChoCodeDomProvider(string codeBlockFilePath, string[] namespaces = null, ChoCodeProviderLanguage language = ChoCodeProviderLanguage.CSharp) { ChoGuard.ArgumentNotNullOrEmpty(codeBlockFilePath, "codeBlockFilePath"); if (!ChoFile.Exists(codeBlockFilePath)) { throw new ArgumentException("{0} file not exists.".FormatString(codeBlockFilePath)); } _statements = File.ReadAllLines(codeBlockFilePath); AddNamespaces(namespaces); }
public virtual void Log(bool condition, string msg) { if (condition) { try { ChoFile.WriteLine(Path.Combine(LogDirectory, LogFileName), String.Format("{1}{0}{2}", Environment.NewLine, DateTime.Now.ToString(LogTimeStampFormat), msg.ToString())); } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error); } } }
public Encoding GetEncoding(string fileName) { if (_encoding == null) { try { ChoETLLog.Info("Determining '{0}' file encoding...".FormatString(fileName)); Encoding = ChoFile.GetEncodingFromFile(fileName); ChoETLLog.Info("Found '{1}' encoding in '{0}' file.".FormatString(fileName, Encoding)); } catch (Exception ex) { Encoding = Encoding.UTF8; ChoETLLog.Error("Error finding encoding in '{0}' file. Default to UTF8.".FormatString(fileName)); ChoETLLog.Error(ex.Message); } } return(Encoding); }
public Encoding GetEncoding(Stream inStream) { if (_encoding == null) { try { ChoETLLog.Info("Determining file encoding..."); Encoding = ChoFile.GetEncodingFromStream(inStream); ChoETLLog.Info("Found {0} encoding in file.".FormatString(Encoding)); } catch (Exception ex) { Encoding = Encoding.UTF8; ChoETLLog.Error("Error finding encoding in file. Default to UTF8."); ChoETLLog.Error(ex.Message); } } return(Encoding); }
private object GetObject(string fileName, ChoBufferProfileEx outerProfile) { object convertedObject = null; using (ChoBufferProfileEx fileProfile = new ChoBufferProfileEx("Converting {0} file to objects...", fileName, outerProfile)) { if (!File.Exists(fileName)) { outerProfile.AppendLine("{0} file not exists.", fileName); } try { object element = ChoFile.GetObject(fileName); if (element != null) { if (element.GetType() != ObjElementType) { throw new ChoApplicationException(String.Format("Unexpected member element. Expected: {0}. Actual: {1}", ObjElementType.FullName, element.GetType().FullName)); } convertedObject = element; } } catch (Exception innerEx) { fileProfile.Append(innerEx); } //REDO: //string configSectionName = ChoConfigurationManager.GetConfigSectionName(Target.GetType()); //if (!String.IsNullOrEmpty(configSectionName) && ChoConfigurationManager.GetConfigEntryByConfigSectionName(configSectionName) != null) // ChoConfigurationManager.GetConfigEntryByConfigSectionName(configSectionName).IncludeFileList.Add(fileName); } return(convertedObject); }
private static void OpenConfiguration(string appConfigPath, bool doBackup) { if (_appConfigPath != appConfigPath) { Trace.TraceInformation("Using AppConfigPath: {0}".FormatString(appConfigPath)); } _appConfigPath = appConfigPath; if (_appConfigPath.IsNullOrWhiteSpace()) { Trace.TraceError("Empty AppConfigPath passed."); return; } ChoFile.SetReadOnly(_appConfigPath, false); if (_configurationChangeWatcher != null) { RestoreAppConfig(); //_configurationChangeWatcher.StopWatching(); } //if (_systemConfigurationChangeWatcher != null) //{ // _systemConfigurationChangeWatcher.StopWatching(); // _systemConfigurationChangeWatcher = null; //} ChoXmlDocument.CreateXmlFileIfEmpty(_appConfigPath); //backup the current configuration string backupConfigFilePath = String.Format("{0}.{1}", _appConfigPath, ChoReservedFileExt.Cho); try { LoadConfigurationFile(); if (doBackup) { if (File.Exists(backupConfigFilePath)) { File.SetAttributes(backupConfigFilePath, FileAttributes.Archive); } File.Copy(_appConfigPath, backupConfigFilePath, true); if (File.Exists(backupConfigFilePath)) { File.SetAttributes(backupConfigFilePath, FileAttributes.Hidden); } } } catch (Exception ex) { Trace.TraceError(ex.ToString()); if (_appXmlDocument != null) { _appXmlDocument.Dispose(); _appXmlDocument = null; } try { //Rollback the configuration file if (doBackup) { File.Copy(backupConfigFilePath, _appConfigPath, true); } LoadConfigurationFile(); } catch { } } finally { if (_configurationChangeWatcher != null) { _configurationChangeWatcher.StartWatching(); } if (_systemConfigurationChangeWatcher != null) { _systemConfigurationChangeWatcher.StartWatching(); } } }
internal static void OpenExeConfiguration() { //Expand Application configuration file string appConfigPath = AppConfigFilePath; // AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; if (!File.Exists(appConfigPath)) { return; } ChoFile.SetReadOnly(appConfigPath, false); if (_configurationChangeWatcher != null) { RestoreAppConfig(); _configurationChangeWatcher.StopWatching(); _configurationChangeWatcher = null; } //backup the current configuration string backupConfigFilePath = String.Format("{0}.cho", appConfigPath); File.Copy(appConfigPath, backupConfigFilePath, true); try { _appXmlDocument = new ChoXmlDocument(appConfigPath, false, true); _appIncludeConfigFilePaths = _appXmlDocument.IncludeFiles; if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0) { _appXmlDocument.XmlDocument.Save(appConfigPath); } _configurationChangeWatcher = new ChoAppConfigurationChangeFileWatcher(ChoConfigurationManager.AppConfigFilePath, "Configurations"); _configurationChangeWatcher.SetConfigurationChangedEventHandler(_key, new ChoConfigurationChangedEventHandler(_configurationChangeWatcher_ConfigurationChanged)); //Remove namespaces if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0) { XDocument doc = XDocument.Load(appConfigPath, LoadOptions.PreserveWhitespace); doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove(); doc.Save(appConfigPath, SaveOptions.DisableFormatting); } _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //TODO: use XmlDocument to expand if (ChoEnvironmentSettings.HasAppConfigPathSpecified()) { appConfigPath = ChoEnvironmentSettings.GetAppConfigPath(); if (!String.IsNullOrEmpty(appConfigPath)) { _configuration = ConfigurationManager.OpenExeConfiguration(appConfigPath); } } } catch { //Rollback the configuration file File.Copy(backupConfigFilePath, appConfigPath, true); throw; } }