windows symbolic link target

前端 未结 6 1064
死守一世寂寞
死守一世寂寞 2020-12-13 19:48

Say that I set up a symbolic link:

mklink  /D C:\\root\\Public\\mytextfile.txt C:\\root\\Public\\myothertextfile.txt

Editor\'s note: O

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 20:30

    Maybe someone's interested in a C# method to resolve all directory symlinks in a directory similar to Directory.GetDirectories(). To list Junctions or File symlinks, simply change the regex.

    static IEnumerable GetAllSymLinks(string workingdir)
    {
        Process converter = new Process();
        converter.StartInfo = new ProcessStartInfo("cmd", "/c dir /Al") { RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = workingdir };
        string output = "";
        converter.OutputDataReceived += (sender, e) =>
        {
            output += e.Data + "\r\n";
        };
        converter.Start();
        converter.BeginOutputReadLine();
        converter.WaitForExit();
    
        Regex regex = new Regex(@"\n.*\\s(.*)\s\[(.*)\]\r");
    
        var matches = regex.Matches(output);
        foreach (Match match in matches)
        {
            var name = match.Groups[1].Value.Trim();
            var target = match.Groups[2].Value.Trim();
            Console.WriteLine("Symlink: " + name + " --> " + target);
    
            yield return new Symlink() { Name = name, Target = target };
        }
    }
    
    class Symlink
    {
        public string Name { get; set; }
        public string Target { get; set; }
    }
    

提交回复
热议问题