我知道你可以获得映射驱动器的路径(例如Find UNC path of a network drive?),但是如果我唯一拥有的只是共享文件夹的路径呢?
例如,假设我有一位朋友通过网络共享文件夹row.item
。我可以在路径C:\MyDocs\PublicDoc
下访问它。我是否有办法在另一台计算机上确定\\danas-pc\PublicDoc
实际映射到\\danas-pc\PublicDoc
?
我问,因为我获得了一个包含路径的日志文件的路径(例如\\danas-pc\c$\MyDocs\PublicDoc
),我需要检查它是否与在另一个位置设置的路径匹配。另一个位置有"短路径" (例如\danas-pc\c$\MyDocs\PublicDoc\mylog.log
),因此,即使日志路径指向相同的位置,程序也会确定它们是不同的。我想看看是否有办法弄清楚他们是指向同一个位置。
答案 0 :(得分:3)
我无法想象为什么你可能需要这个,因为远程实例的完整路径是\ danas-pc \ PublicDoc但是如果你让你的想象力茁壮成长我会建议这样的事情:
(1)在共享文件夹内的远程计算机上,您可以删除一个小脚本,如果执行则返回完整路径。您必须搜索适用于Windows或Linux环境的编码,您还需要具有执行权限或权限。例如,在Windows上,你可以在linux中使用vbscrit或cscript和.sh脚本。
另请注意,从远程主机看到它,就远程主机而言,完整路径是\ NAME-OR-IP \ Path \ to \ Folder \或\ File等。对于您的远程连接是完整路径;)
更新: 根据下面的评论,这是一个完整的脚本,它执行以下操作
假设:您对网络文件夹具有读/写访问权限
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. https://stackoverflow.com/questions/2129327/how-to-get-the-fully-qualified-path-for-a-file-in-vbscript
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. https://stackoverflow.com/questions/27050195/how-do-i-get-the-output-from-my-vbscript-console-using-c
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);
}
}
}
不幸的是,当远程执行时,脚本会回复网络路径:(太失望了......真的很抱歉!只要从远程系统外的用户执行执行,它就会回复与该实例相关的绝对路径。认为内部流程/用户应该执行该文件并回复应用程序的答案。
我会尝试明天再想些什么,如果我很幸运,也许可以回复。