/// <summary> /// Creates a relative path from one file or folder to another. /// </summary> /// <param name="fromPath">Contains the directory that defines the start of the relative path.</param> /// <param name="fromIs">Is the fromPath a File or a Folder</param> /// <param name="toPath">Contains the path that defines the endpoint of the relative path.</param> /// <param name="toIs">Is the toPath a File or a Folder</param> /// <returns>The relative path from the start directory to the end path or <c>toPath</c> if the paths are not related.</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="UriFormatException"></exception> /// <exception cref="InvalidOperationException"></exception> public static String MakeRelativePath(String fromPath, PathIs fromIs, String toPath, PathIs toIs) { if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath"); if (String.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath"); //Slash am Ende anfügen, damit Uri damit klarkommt und weiß, was ein Folder ist, und was nicht if (!fromPath.EndsWith(Path.DirectorySeparatorChar.ToString()) && !fromPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()) && fromIs == PathIs.Folder) fromPath += Path.DirectorySeparatorChar; if (!toPath.EndsWith(Path.DirectorySeparatorChar.ToString()) && !toPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()) && toIs == PathIs.Folder) toPath += Path.DirectorySeparatorChar; Uri fromUri = new Uri(fromPath); Uri toUri = new Uri(toPath); if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative. Uri relativeUri = fromUri.MakeRelativeUri(toUri); String relativePath = Uri.UnescapeDataString(relativeUri.ToString()); if (toUri.Scheme.ToUpperInvariant() == "FILE") { relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); } if (relativePath == string.Empty) relativePath = ".\\"; //ein \ am Ende entfernen, dies macht Probleme, insbesondere in CommandLine wenn quoted //zudem scheint der .Net - Standard zu sein, kein \ am Ende zu haben vgl. Path.GetDirectoryname() return relativePath.TrimEnd(Path.DirectorySeparatorChar); }
/// <summary> /// Saves Assembly After Modifications /// </summary> public void SaveContext() { string NewPath = PathIs.Insert(PathIs.Length - 4, "HereWeGo"); // Thx 4 drakoniа#0601 for the insert trick :D if (DnModule != null) { if (DnModule.IsILOnly) { var MangedWriter = new ModuleWriterOptions(DnModule) { Logger = DummyLogger.NoThrowInstance, MetadataOptions = { Flags = MetadataFlags.PreserveAll } }; DnModule.Write(NewPath.Replace("HereWeGo", "-DnLibed"), MangedWriter); Log.Info("Done Saved Manged Dnlib Module"); } else { var UnMangedWriter = new NativeModuleWriterOptions(DnModule, false) { Logger = DummyLogger.NoThrowInstance, MetadataOptions = { Flags = MetadataFlags.PreserveAll } }; DnModule.NativeWrite(NewPath.Replace("HereWeGo", "-DnLibed"), UnMangedWriter); Log.Info("Done Saved Native Dnlib Module"); } } if (AsmModule != null) { var IMPEIB = new ManagedPEImageBuilder() { DotNetDirectoryFactory = new DotNetDirectoryFactory() { MetadataBuilderFlags = MetadataBuilderFlags.PreserveAll, MethodBodySerializer = new CilMethodBodySerializer { ComputeMaxStackOnBuildOverride = false } } }; var IR = IMPEIB.CreateImage(AsmModule); var FBuilder = new ManagedPEFileBuilder(); var File = FBuilder.CreateFile(IR.ConstructedImage); if (!IR.DiagnosticBag.IsFatal) { File.Write(NewPath.Replace("HereWeGo", "-AsmResolved")); // Ignore Errors. } else { AsmModule.Write(NewPath.Replace("HereWeGo", "-AsmResolved"), IMPEIB); } Log.Info("Done Saved AsmResolver Module"); } }
/// <summary> /// Creates a relative path from one file or folder to another. /// </summary> /// <param name="fromPath">Contains the directory that defines the start of the relative path.</param> /// <param name="fromIs">Is the fromPath a File or a Folder</param> /// <param name="toPath">Contains the path that defines the endpoint of the relative path.</param> /// <param name="toIs">Is the toPath a File or a Folder</param> /// <returns>The relative path from the start directory to the end path or <c>toPath</c> if the paths are not related.</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="UriFormatException"></exception> /// <exception cref="InvalidOperationException"></exception> public static string MakeRelativePath(string fromPath, PathIs fromIs, string toPath, PathIs toIs) { if (string.IsNullOrEmpty(fromPath)) { throw new ArgumentNullException(nameof(fromPath)); } if (string.IsNullOrEmpty(toPath)) { throw new ArgumentNullException(nameof(toPath)); } //Slash am Ende anfügen, damit Uri damit klarkommt und weiß, was ein Folder ist, und was nicht if (!fromPath.EndsWith(Path.DirectorySeparatorChar.ToString()) && !fromPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()) && fromIs == PathIs.Folder) { fromPath += Path.DirectorySeparatorChar; } if (!toPath.EndsWith(Path.DirectorySeparatorChar.ToString()) && !toPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()) && toIs == PathIs.Folder) { toPath += Path.DirectorySeparatorChar; } Uri fromUri = new Uri(fromPath); Uri toUri = new Uri(toPath); if (fromUri.Scheme != toUri.Scheme) { return(toPath); } // path can't be made relative. Uri relativeUri = fromUri.MakeRelativeUri(toUri); string relativePath = Uri.UnescapeDataString(relativeUri.ToString()); if (toUri.Scheme.ToUpperInvariant() == "FILE") { relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); } if (relativePath == string.Empty) { relativePath = ".\\"; } //ein \ am Ende entfernen, dies macht Probleme, insbesondere in CommandLine wenn quoted //zudem scheint der .Net - Standard zu sein, kein \ am Ende zu haben vgl. Path.GetDirectoryname() return(relativePath.TrimEnd(Path.DirectorySeparatorChar)); }
/// <summary> /// Initializes a new instance of the <see cref="PathAttribute" /> class. /// </summary> /// <param name="usage"> /// The usage. /// </param> /// <param name="path"> /// The path. /// </param> /// <exception cref="ArgumentException"> /// <paramref name="path" /> must contain a valid path or fragment of a path. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="usage" /> does not contain a valid value. /// </exception> public PathAttribute(PathIs usage, string path) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Exceptions.Validate_Path, nameof(path)), nameof(path)); } switch (usage) { case PathIs.Absolute: case PathIs.Relative: break; default: throw new ArgumentOutOfRangeException(nameof(usage), usage, string.Format(CultureInfo.CurrentCulture, Exceptions.Validate_EnumDoesNotContainValidValue, nameof(usage))); } Usage = usage; Path = path; }
public void MakeRelativePath(string fromPath, PathIs fromIs, string toPath, PathIs toIs, string result) { FileUtils.MakeRelativePath(fromPath, fromIs, toPath, toIs).Should().Be(result); }