public ExtractedDirectoryPath ExtractDirectoryPath(string path) { // Create the return value. ExtractedDirectoryPath retval = new ExtractedDirectoryPath(); // Split the strings. retval.Directory = path.Substring(0, path.LastIndexOf(this.SeparatorChar) + 1); retval.File = path.Substring(path.LastIndexOf(this.SeparatorChar) + 1); // Return the extracted paths. return(retval); }
public ExtractedDirectoryPath ExtractDirectoryPath(string path) { // Create the return value. ExtractedDirectoryPath retval = new ExtractedDirectoryPath(); // Split the strings. retval.Directory = path.Substring(0, path.LastIndexOf(this.SeparatorChar) + 1); retval.File = path.Substring(path.LastIndexOf(this.SeparatorChar) + 1); // Return the extracted paths. return retval; }
private string CleanPath(string path) { // Create a temporary instance of the input path. string tmp = path; // Replace / and \ with the appropriate seperator character. tmp = tmp.Replace("\\", this.SeparatorChar); tmp = tmp.Replace(@"\", this.SeparatorChar); tmp = tmp.Replace("/", this.SeparatorChar); // Are we cleaning a directory or file path? if (this.IsDirectoryPath(tmp)) { // Directory path is simple, just loop through and replace the invalid characters. foreach (char c in Path.GetInvalidPathChars()) { tmp = tmp.Replace(c.ToString(), string.Empty); } } else { // File path is not simple, start by separating the file and directory paths. using (ExtractedDirectoryPath p = this.ExtractDirectoryPath(tmp)) { // Go and clean the directory path. p.Directory = this.CleanPath(p.Directory); // Loop through and replace the invalid file path characters. foreach (char c in Path.GetInvalidFileNameChars()) { p.File = p.File.Replace(c.ToString(), string.Empty); } // Now put the path back together. tmp = p.FullPath; } } return(tmp); }