ZeroInstall.Store.Implementations.FlagUtils.Rename C# (CSharp) Метод

Rename() публичный статический Метод

Adds a directory prefix to all entries in an external flag file.
or is not a relative path. There was an error writing the flag file. You have insufficient rights to write the flag file.
public static Rename ( [ path, [ source, [ destination ) : void
path [ The full path to the flag file, named or .
source [ The old path of the renamed file or directory relative to .
destination [ The new path of the renamed file or directory relative to .
Результат void
        public static void Rename([NotNull] string path, [NotNull] string source, [NotNull] string destination)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));
            if (string.IsNullOrEmpty(source)) throw new ArgumentNullException(nameof(source));
            if (Path.IsPathRooted(source)) throw new ArgumentException(Resources.PathNotRelative, nameof(source));
            if (string.IsNullOrEmpty(destination)) throw new ArgumentNullException(nameof(destination));
            if (Path.IsPathRooted(destination)) throw new ArgumentException(Resources.PathNotRelative, nameof(destination));
            #endregion

            if (!File.Exists(path)) return;

            // Convert paths to rooted Unix-style
            source = "/" + source.Replace(Path.DirectorySeparatorChar, '/');
            destination = "/" + destination.Replace(Path.DirectorySeparatorChar, '/');

            using (var atomic = new AtomicWrite(path))
            using (var newFlagFile = new StreamWriter(atomic.WritePath, append: false, encoding: FeedUtils.Encoding) {NewLine = "\n"})
            using (var oldFlagFile = File.OpenText(path))
            {
                // Each line in the file signals a flagged file
                while (!oldFlagFile.EndOfStream)
                {
                    string line = oldFlagFile.ReadLine();
                    if (line != null && line.StartsWith("/"))
                    {
                        if (line == source || line.StartsWith(source + "/"))
                            newFlagFile.WriteLine(destination + line.Substring(source.Length));
                        else newFlagFile.WriteLine(line);
                    }
                }
                atomic.Commit();
            }
        }
        #endregion

Usage Example

Пример #1
0
        public void TestRename()
        {
            using (var flagFile = new TemporaryFile("0install-unit-tests"))
            {
                File.WriteAllText(flagFile, "/dir/file1\n/dir/file2\n/dir2/file\n");

                FlagUtils.Rename(flagFile, "dir", "new_dir");
                File.ReadAllText(flagFile).Should().Be("/new_dir/file1\n/new_dir/file2\n/dir2/file\n");
            }
        }
All Usage Examples Of ZeroInstall.Store.Implementations.FlagUtils::Rename