以编程方式获取另一台计算机上共享文件夹的完整路径?

Sai*_*irl 3 c# networking unc mapped-drive network-drive

我知道您可以获得映射驱动器的路径(例如,查找网络驱动器的 UNC 路径?),但是如果我只有共享文件夹的路径怎么办?

例如,假设我有一个朋友正在C:\MyDocs\PublicDoc通过网络共享该文件夹。我可以在 path 下访问它\\danas-pc\PublicDoc。有什么方法可以让我在另一台计算机上确定\\danas-pc\PublicDoc实际映射到\\danas-pc\c$\MyDocs\PublicDoc

我问是因为我得到了一个具有路径(例如\danas-pc\c$\MyDocs\PublicDoc\mylog.log)的日志文件的路径,我需要检查它是否与在另一个位置设置的相同路径匹配。另一个位置具有“短路径”(例如\\danas-pc\PublicDoc\mylog.log),因此,即使日志路径指向相同的位置,程序也会确定它们是不同的。我想看看是否有办法确定它们指向同一个位置。

oet*_*oni 5

我无法想象为什么你可能需要这个,因为远程实例的完整路径是 \danas-pc\PublicDoc 但如果你让你的想象力蓬勃发展,我会建议这样的:

(1) 在共享文件夹内的远程计算机上,您可以放置​​一个小脚本,如果执行该脚本返回完整路径。您必须为 windows 或 linux 环境搜索适当的编码,还需要对其具有执行权限或权限。例如,在 Windows 上,您可以在 linux 中拥有一个 vbscrit 或 cscript 和一个 .sh 脚本。

另请注意,从远程主机查看,就远程主机而言,完整路径为 \NAME-OR-IP\Path\to\Folder\or\File 等。对于您在远程连接上的完整路径;)


更新:根据下面的评论,这是一个完整的脚本,执行以下操作

  1. 创建一个带有代码的 vbscript 来检索当前的完整路径
  2. 将文件复制到网络所需的路径
  3. 执行 vbscript 并读回结果
  4. 删除 vbscript

假设:您对网络文件夹具有读/写访问权限

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace GetNetworkFullPath
{
    class Program
    {
        static void Main(string[] args)
        {
            var networkFolder = "\\\\REMOTE-PC-NAME\\SharedFolder";
            var nameOfVBScript = "capturepath.vbs";
            var vbsOutput = "";

            //Get the name of the current directory
            var currentDirectory = Directory.GetCurrentDirectory();
            Console.WriteLine("Current Dir: " + currentDirectory);


            //1. CREATE A VBSCRIPT TO OUTPUT THE PATH WHERE IT IS PRESENT
            //Ref. /sf/ask/149052921/
            var vbscriptToExecute = "Dim folderName \n" +
                                        "folderName = \"\" \n" +
                                        "Dim fso \n" +
                                        "Set fso = CreateObject(\"Scripting.FileSystemObject\") \n" +
                                        "Dim fullpath \n" +
                                        "fullpath = fso.GetAbsolutePathName(folderName) \n" +
                                        "WScript.Echo fullpath \n";


            //Write that script into a file into the current directory
            System.IO.File.WriteAllText(@""+ nameOfVBScript  + "", vbscriptToExecute);


            //2. COPY THE CREATED SCRIPT INTO THE NETWORK PATH
            //Ref. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-copy-delete-and-move-files-and-folders
            string sourceFile = System.IO.Path.Combine(currentDirectory, nameOfVBScript);
            string destFile = System.IO.Path.Combine(networkFolder, nameOfVBScript);

            System.IO.File.Copy(sourceFile, destFile, true);

            //3. EXECUTE THAT SCRIPT AND READ THE OUTPUT
            //Ref. /sf/ask/1893513681/
            Process scriptProc = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.WorkingDirectory = @"" + networkFolder + "";
            info.FileName = "Cscript.exe";
            info.Arguments = nameOfVBScript;
            info.RedirectStandardError = true;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            scriptProc.StartInfo = info;
            scriptProc.Start();
            scriptProc.WaitForExit();
            bool exit = false;

            while (!scriptProc.StandardOutput.EndOfStream)
            {
                vbsOutput = scriptProc.StandardOutput.ReadLine();
            }

            Console.WriteLine("vbscript says: " + vbsOutput);


            //4. DELETE THE FILE YOU JUST COPIED THERE
            System.IO.File.Delete(@"" + networkFolder + "\\" + nameOfVBScript);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,当远程执行时,脚本会回复网络路径 :( 太失望了......真的很抱歉!只要远程系统之外的用户执行,它就会回复与该实例相关的绝对路径。我认为是内部进程/用户应该执行文件并回复应用程序的答案。

在此处输入图片说明

明天我会试着想更多的东西,如果我幸运的话也许会回复。