从FolderBrowserDialog获取网络路径而不是驱动器

从FolderBrowserDialog获取网络路径而不是驱动器

本文介绍了从FolderBrowserDialog获取网络路径而不是驱动器号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有FolderBrowserDialog的winform,可以从网络驱动器中选择一个文件夹.问题是它返回驱动器号(X:\Folder...)而不是网络路径(\\Network\Projects\Folder...).如何获取网络路径?

I have a winform with a FolderBrowserDialog to choose a folder from a network drive. The issue is that it returns the drive letter (X:\Folder...) rather than the network path (\\Network\Projects\Folder...). How can I get the network path?

推荐答案

快速而又肮脏的代码,会将该路径显示为MessageBox中的网络路径.您可能需要添加其他检查和/或对此进行一些重组.

Quick and dirty code that will show the path as a network path in a MessageBox. You may want to add additional checks and/or restructure this a bit.

        var dialog = new OpenFileDialog();
        dialog.ShowDialog();

        var path = dialog.FileName;
        using (var managementObject = new ManagementObject())
        {
            managementObject.Path = new ManagementPath($"Win32_LogicalDisk='{path.Substring(0,2)}'");
            var driveType = (DriveType)(uint)managementObject["DriveType"];
            var networkPath = Convert.ToString(managementObject["ProviderName"]);


            if (driveType == DriveType.Network)
            {
                MessageBox.Show(path.Replace(path.Substring(0, 3), networkPath));
            }
        }

        MessageBox.Show(path);

这篇关于从FolderBrowserDialog获取网络路径而不是驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:56