Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从符号链接C获取实际路径#_C# - Fatal编程技术网

C# 从符号链接C获取实际路径#

C# 从符号链接C获取实际路径#,c#,C#,有人知道如何从symlink文件或文件夹中获取实际路径吗?谢谢大家! 大家好,在我的研究之后,我找到了这个解决方案,用于获取符号链接的实际路径。如果您创建了符号链接,并希望检查此文件或文件夹的实际指针在哪里。如果有人有更好的方法写,请分享 [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)] private static ext

有人知道如何从symlink文件或文件夹中获取实际路径吗?谢谢大家!

大家好,在我的研究之后,我找到了这个解决方案,用于获取符号链接的实际路径。如果您创建了符号链接,并希望检查此文件或文件夹的实际指针在哪里。如果有人有更好的方法写,请分享

    [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);

    [DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int GetFinalPathNameByHandle([In] SafeFileHandle hFile, [Out] StringBuilder lpszFilePath, [In] int cchFilePath, [In] int dwFlags);

    private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
    private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;


    public static string GetRealPath(string path)
    {
        if (!Directory.Exists(path) && !File.Exists(path))
        {
            throw new IOException("Path not found");
        }

        SafeFileHandle directoryHandle = CreateFile(path, 0, 2, IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero); //Handle file / folder

        if (directoryHandle.IsInvalid)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        StringBuilder result = new StringBuilder(512);
        int mResult = GetFinalPathNameByHandle(directoryHandle, result, result.Capacity, 0);

        if (mResult < 0)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        if (result.Length >= 4 && result[0] == '\\' && result[1] == '\\' && result[2] == '?' && result[3] == '\\')
        {
            return result.ToString().Substring(4); // "\\?\" remove
        }
        return result.ToString();
     }
[DllImport(“kernel32.dll”,EntryPoint=“CreateFileW”,CharSet=CharSet.Unicode,SetLastError=true)]
私有静态外部安全文件句柄CreateFile(字符串lpFileName、int-dwDesiredAccess、int-dwShareMode、IntPtr-securityAttributes、int-dwCreationDisposition、int-dwFlagsAndAttributes、IntPtr-hTemplateFile);
[DllImport(“kernel32.dll”,EntryPoint=“GetFinalPathNameByHandleW”,CharSet=CharSet.Unicode,SetLastError=true)]
私有静态extern int GetFinalPathNameByHandle([In]SafeFileHandle hFile,[Out]StringBuilder lpszFilePath,[In]int-cchFilePath,[In]int-dwFlags);
私有const int CREATION\u DISPOSITION\u OPEN\u EXISTING=3;
private const int FILE\u FLAG\u BACKUP\u SEMANTICS=0x020000000;
公共静态字符串GetRealPath(字符串路径)
{
如果(!Directory.Exists(path)&&!File.Exists(path))
{
抛出新IOException(“未找到路径”);
}
SafeFileHandle directoryHandle=CreateFile(路径,0,2,IntPtr.Zero,创建\处置\打开\存在,文件\标志\备份\语义,IntPtr.Zero);//句柄文件/文件夹
if(directoryHandle.IsInvalid)
{
抛出新的Win32Exception(Marshal.GetLastWin32Error());
}
StringBuilder结果=新的StringBuilder(512);
int mResult=GetFinalPathNameByHandle(directoryHandle,result,result.Capacity,0);
如果(mResult<0)
{
抛出新的Win32Exception(Marshal.GetLastWin32Error());
}
如果(result.Length>=4&&result[0]='\\'&&result[1]='\\'&&result[2]='?'&&result[3]='\')
{
返回结果.ToString().Substring(4);/“\\?\”删除
}
返回result.ToString();
}

请将此问题改写为一个问题,谢谢@Martheen提供此信息!似乎是重复的,而不是执行
directoryHandle.DangerousGetHandle()
我认为您可以将签名更改为
private static extern int GetFinalPathNameByHandle([In]SafeFileHandle hFile,…