[英]Programmatically get the full path of a shared folder on another computer?
我知道你可以獲得映射驅動器的路徑(例如, 查找網絡驅動器的UNC路徑? ),但是如果我唯一擁有的只是共享文件夾的路徑呢?
例如,假設我有一位朋友通過網絡共享文件夾C:\\MyDocs\\PublicDoc
。 我可以在路徑\\\\danas-pc\\PublicDoc
下訪問它。 有什么方法可以,在另一台計算機上,確定\\\\danas-pc\\PublicDoc
實際上映射到\\\\danas-pc\\c$\\MyDocs\\PublicDoc
?
我問,因為我有一個路徑到一個具有路徑的日志文件(例如\\danas-pc\\c$\\MyDocs\\PublicDoc\\mylog.log
),我需要檢查它是否匹配在另一個路徑中設置的相同路徑地點。 另一個位置具有“短路徑”(例如\\\\danas-pc\\PublicDoc\\mylog.log
),因此,即使日志路徑指向相同的位置,程序也會確定它們是不同的。 我想知道是否有辦法弄清楚他們指向的是同一個位置。
我無法想象為什么你可能需要這個,因為遠程實例的完整路徑是\\ 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);
}
}
}
不幸的是,當遠程執行時,腳本回復網絡路徑:(很失望......真的很抱歉!只要從遠程系統外的用戶執行執行,它將回復與該實例相關的絕對路徑。我認為是內部的進程/用戶應執行該文件並回復應用程序的答案。
我明天會想更多的事情,如果我很幸運,可能會回復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.