/// <summary> /// meke filePath relative to currentPath. /// If is set inCurrentDir path is converted to relative only /// if currentPath is parent of filePath</summary> public static string MakeRelative(string filePath, string currentPath, bool inCurrentDir = true) { filePath = filePath.Trim(); currentPath = currentPath.Trim(); if (currentPath == "") { return(filePath); } if (!Os.FileExists(filePath) && !Os.DirectoryExists(filePath)) { return(filePath); } filePath = Os.GetFullPath(filePath); if (Os.FileExists(currentPath)) { currentPath = Os.GetDirectoryName(currentPath); } if (!Os.DirectoryExists(currentPath)) { return(filePath); } currentPath = Os.GetFullPath(currentPath); Uri pathUri = new Uri(filePath); // Folders must end in a slash if (!currentPath.EndsWith(Path.DirectorySeparatorChar.ToString())) { currentPath += Path.DirectorySeparatorChar; } int pos = filePath.ToLower().IndexOf(currentPath.ToLower()); if (inCurrentDir && pos != 0) // skip files outside of currentPath { return(filePath); } Uri folderUri = new Uri(currentPath); return(Uri.UnescapeDataString( folderUri.MakeRelativeUri(pathUri) .ToString() .Replace('/', Path.DirectorySeparatorChar) )); }
/// <summary> /// open path in system if exist </summary> public static void OpenPathInSystem(string path) { if (Os.FileExists(path)) // OPEN FILE { try { string parent_diectory = Os.GetFileDirectory(path); System.Diagnostics.Process.Start(parent_diectory); } catch (Exception ex) { Program.log.Write("openPathInSystem open file: error:" + ex.Message); } } else if (Os.DirectoryExists(path)) // OPEN DIRECTORY { try { System.Diagnostics.Process.Start(path); } catch (Exception ex) { Program.log.Write("openPathInSystem open directory: error:" + ex.Message); } } }
/*************************************************************************************************************************/ // PLUGINS /// <summary> /// load plugins from assebmblies</summary> public void LoadPugins() { // load external plugins UID9841812564 plugins = new Plugins(); // load plugins from current application directory (portable mode) string pluginsLocalDirectory = Os.Combine(Os.GetCurrentApplicationDirectory(), this.pluginsDirectoryName); if (Os.DirectoryExists(pluginsLocalDirectory)) { plugins.LoadPlugins(pluginsLocalDirectory); } #if !DEBUG // load plugins from global plugins directory string pluginsGlobalDirectory = Os.Combine(this.programOptionsFile.GetGlobalConfigDirectory(), this.pluginsDirectoryName); if (Os.DirectoryExists(pluginsGlobalDirectory)) { plugins.LoadPlugins(pluginsGlobalDirectory); } #endif }
/*************************************************************************************************************************/ /// <summary> /// load global config file from portable file configuration or global file configuration /// </summary> /// <param name="parameters">reference to parameter object</param> public ProgramOptionsFile(ProgramOptions programOptions) { this.programOptions = programOptions; // use local config file this.optionsFilePath = Os.Combine(Os.GetCurrentApplicationDirectory(), this.configFileName); // use global config file if local version not exist if (!Os.FileExists(this.optionsFilePath)) { this.optionsFilePath = Os.Combine( this.GetGlobalConfigDirectory(), this.configFileName ); } // open config file if exist if (Os.FileExists(this.optionsFilePath)) { this.LoadConfigFile(); } else { string globalConfigDirectory = Os.Combine( Os.GetApplicationsDirectory(), this.configFileDirectory ); // create global config directory if not exist if (!Os.DirectoryExists(globalConfigDirectory)) { Os.CreateDirectory(globalConfigDirectory); } // if config file dosn't exist create one with default values this.SaveConfigFile(); } }
/*************************************************************************************************************************/ // DIRECTORY OPERATIONS /// <summary> /// check if path is directory</summary> public static bool IsDirectory(string path) { return(Os.DirectoryExists(path)); }
/// <summary> /// decompress string with directory structure to path</summary> public static void DecompressPath(string compressedData, string destinationPath) { if (!Os.DirectoryExists(destinationPath)) { return; } destinationPath = Os.NormalizedFullPath(destinationPath); string xml = Unzip(compressedData); XmlReaderSettings xws = new XmlReaderSettings { CheckCharacters = false }; string version = ""; List <EDirectory> directories = new List <EDirectory>(); List <EFile> files = new List <EFile>(); try { using (XmlReader xr = XmlReader.Create(new StringReader(xml), xws)) { XElement xRoot = XElement.Load(xr); if (xRoot.Name.ToString() == "archive") { foreach (XElement xEl in xRoot.Elements()) { if (xEl.Name.ToString() == "version") { version = xEl.Value; } if (xEl.Name.ToString() == "directories") { foreach (XElement xDirectory in xEl.Descendants()) { if (xDirectory.Name.ToString() == "directory") { string name = ""; foreach (XElement xData in xDirectory.Descendants()) { if (xData.Name.ToString() == "name") { name = xData.Value; } } if (name.Trim() != "") { EDirectory eDirectory = new EDirectory { name = name }; directories.Add(eDirectory); } } } } if (xEl.Name.ToString() == "files") { foreach (XElement xFile in xEl.Descendants()) { if (xFile.Name.ToString() == "file") { string name = ""; string data = ""; foreach (XElement xData in xFile.Descendants()) { if (xData.Name.ToString() == "name") { name = xData.Value; } if (xData.Name.ToString() == "data") { data = xData.Value; } } if (name.Trim() != "" && data.Trim() != "") { EFile eFile = new EFile { name = name, data = data }; files.Add(eFile); } } } } } } } } catch (Exception ex) { Program.log.Write("decompress file xml error: " + ex.Message); } foreach (EDirectory directory in directories) { string newDirPath = Os.Combine(destinationPath, directory.name); if (!Os.Exists(newDirPath)) { Os.CreateDirectory(newDirPath); } } foreach (EFile file in files) { string newFilePath = Os.Combine(destinationPath, file.name); if (!Os.Exists(newFilePath)) { Os.WriteAllBytes( newFilePath, Convert.FromBase64String( file.data ) ); } } // process dirrectories create to path // process files create to path }