简体   繁体   English

如何从C#获取虚拟驱动器中文件的物理路径

[英]How to get the physical path of a file that is in a virtual drive from C#

The problem is posed in the following way: 出现此问题的方式如下:

I create a virtual drive or "virtual drive letter" with a command in DOS called subst by means of a bat like this: 我用这样的蝙蝠在DOS中用一个称为subst的命令创建一个虚拟驱动器或“虚拟驱动器号”:

@echo off
cd \
subst J: /D
subst J: C:\Virtual_Disk\Unit_J

In the list of windows units I see the unit created,in which I placed some files and an executable. 在Windows单元列表中,我看到创建的单元,在其中放置了一些文件和可执行文件。 I am currently developing a program in c # where it calls the path of the executable that is in the virtual path, and suddenly, an exception is thrown that indicates that the path of the executable is not found even though the executable is there, I look at the properties of the executable. 我目前正在用c#开发一个程序,在其中调用虚拟路径中的可执行文件的路径,突然,抛出一个异常,指示即使该可执行文件在该位置也找不到可执行文件的路径,我查看可执行文件的属性。 executable and in the field location indicates the virtual path of that executable but not the physical route. 可执行文件,并且在字段中的位置指示该可执行文件的虚拟路径,而不是物理路径。 If I was not clear, I will give an example: 如果不清楚,我将举一个例子:

(1) virtual path of the executable (1)可执行文件的虚拟路径

J:\Program\executable.exe

(2) physical route where the executable is located (2)可执行文件所在的物理路径

C:\Virtual_Disks\Unit_J\Program\executable.exe

C# captures the virtual path of the executable (1) but not where it really is (2). C#捕获可执行文件的虚拟路径(1),但不捕获其实际位置(2)。

Not being more, I appreciate the help provided. 不多,我感谢提供的帮助。

This should work, from here : 这里可以正常工作:

    [DllImport("kernel32.dll")]
    private static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);

    private static string GetPhysicalPath(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            throw new ArgumentNullException("path");
        }

        // Get the drive letter
        var pathRoot = Path.GetPathRoot(path);
        if (string.IsNullOrEmpty(pathRoot))
        {
            throw new ArgumentNullException("path");
        }

        var substPrefix = @"\??\";
        var lpDeviceName = pathRoot.Replace(@"\", string.Empty);            
        var lpTargetPath = new StringBuilder(260);
        if (QueryDosDevice(lpDeviceName, lpTargetPath, lpTargetPath.Capacity) != 0)
        {
            string result;

            // If drive is substed, the result will be in the format of "\??\C:\RealPath\".
            if (lpTargetPath.ToString().StartsWith(substPrefix))
            {
                // Strip the \??\ prefix.
                var root = lpTargetPath.ToString().Remove(0, substPrefix.Length);
                result = Path.Combine(root, path.Replace(Path.GetPathRoot(path), string.Empty));
            }

            else
            {
                // if not SUBSTed, just assume it's not mapped.
                result = path;
            }

            return result;
        }
        else
        {
            return null;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM