added methods for symlink creation

This commit is contained in:
Timerix22 2021-11-13 22:50:17 +03:00
parent 9b41e2230c
commit 861346456c
2 changed files with 20 additions and 4 deletions

View File

@ -136,10 +136,24 @@ namespace DTLib.Filesystem
dirInfo.SetAccessControl(dirSecurity);
}
public static void CreateSymlink(string symlinkName, string sourceName)
public static void CreateSymlink(string sourceName, string symlinkName)
{
if (symlinkName.Contains("\\"))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\')));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
throw new InvalidOperationException($"some error occured while creating symlink\nCreateSymlink({symlinkName}, {sourceName})");
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})");
}
// copies directory with symlinks instead of files
public static int SymCopy(string srcdir, string newdir)
{
var files = Directory.GetAllFiles(srcdir);
if (!srcdir.EndsWith('\\')) srcdir += '\\';
if (!newdir.EndsWith('\\')) newdir += '\\';
int i = 0;
for (; i < files.Count; i++)
File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir));
return i;
}
}
}

View File

@ -75,10 +75,12 @@ namespace DTLib.Filesystem
return System.IO.File.Open(file, System.IO.FileMode.Append);
}
public static void CreateSymlink(string symlinkName, string sourceName)
public static void CreateSymlink(string sourceName, string symlinkName)
{
if (symlinkName.Contains("\\"))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\')));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
throw new InvalidOperationException($"some error occured while creating symlink\nCreateSymlink({symlinkName}, {sourceName})");
throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})");
}
}
}