代码之家  ›  专栏  ›  技术社区  ›  Chris J

从本地路径或映射路径获取UNC路径

  •  10
  • Chris J  · 技术社区  · 16 年前

    C: \文件夹\文本.txt->C:\文件夹\文本.txt
    L: \文件夹\Sample.txt->\\server\Folder1\Folder\Sample.txt其中L:映射到\\server\Folder1\
    \\服务器\文件夹\样本.odf->\server\文件夹\Sample.odf

    在C#中有没有一种简单的方法可以做到这一点,或者我必须使用windows api调用WNetGetConnection,然后手动检查那些不会被映射的方法?

    4 回复  |  直到 16 年前
        1
  •  2
  •   JaredPar    16 年前

    这是一些带有包装函数LocalToUNC的C#代码,它似乎工作正常,尽管我还没有对其进行广泛的测试。

        [DllImport("mpr.dll")]
        static extern int WNetGetUniversalNameA(
            string lpLocalPath, int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize
        );
    
        // I think max length for UNC is actually 32,767
        static string LocalToUNC(string localPath, int maxLen = 2000)
        {
            IntPtr lpBuff;
    
            // Allocate the memory
            try
            {
                lpBuff = Marshal.AllocHGlobal(maxLen); 
            }
            catch (OutOfMemoryException)
            {
                return null;
            }
    
            try
            {
                int res = WNetGetUniversalNameA(localPath, 1, lpBuff, ref maxLen);
    
                if (res != 0)
                    return null;
    
                // lpbuff is a structure, whose first element is a pointer to the UNC name (just going to be lpBuff + sizeof(int))
                return Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(lpBuff));
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                Marshal.FreeHGlobal(lpBuff);
            }
        }
    
        2
  •  5
  •   Jay Riggs    16 年前

    WNetGetUniversalName() .

    我已经修改过了 this code

        3
  •  5
  •   Jack Culhane    12 年前

    BCL中没有内置的功能来执行等效操作。我认为你最好的选择是按照你的建议登录WNetGetConnection。

        4
  •  0
  •   RRUZ    16 年前

    德尔菲。净

    你必须把它翻译成c#

    function WNetGetUniversalName; external;
    [SuppressUnmanagedCodeSecurity, DllImport(mpr, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'WNetGetUniversalNameA')]
    
    
    function ExpandUNCFileName(const FileName: string): string;
    
    function GetUniversalName(const FileName: string): string;
    const
    UNIVERSAL_NAME_INFO_LEVEL = 1;    
    var
      Buffer: IntPtr;
      BufSize: DWORD;
    begin
      Result := FileName;
      BufSize := 1024;
      Buffer := Marshal.AllocHGlobal(BufSize);
      try
        if WNetGetUniversalName(FileName, UNIVERSAL_NAME_INFO_LEVEL,
          Buffer, BufSize) <> NO_ERROR then Exit;
        Result := TUniversalNameInfo(Marshal.PtrToStructure(Buffer,
          TypeOf(TUniversalNameInfo))).lpUniversalName;
      finally
        Marshal.FreeHGlobal(Buffer);
      end;
    end;
    
    begin
      Result :=System.IO.Path.GetFullPath(FileName);
      if (Length(Result) >= 3) and (Result[2] = ':') and (Upcase(Result[1]) >= 'A')
        and (Upcase(Result[1]) <= 'Z') then
        Result := GetUniversalName(Result);
    end;