我不能想象爲什麼需要這個,因爲遠程實例的完整路徑是\達納斯-PC \ PublicDoc但如果你讓你的想象力蓬勃發展,我建議是這樣的:
(1)在共享文件夾內的遠程計算機上,您可以放置一個小腳本,該腳本在執行時返回完整路徑。您必須搜索適當的Windows或Linux環境編碼,您還需要擁有執行權限或權限。例如在windows上,你可以在linux中使用vbscrit或cscript和.sh腳本。
另請注意,從遠程主機來看,就遠程主機而言,完整路徑是\ NAME-OR-IP \ Path \ to \ Folder \或\ File等。對於遠程連接是完整路徑;)
UPDATE: 按下面的評論,這是做以下
- 一個完整的腳本創建與它的代碼VBScript來檢索當前的完整路徑
- 複製文件進入網絡所需的路徑
- 執行VBScript和讀取結果返回
- 刪除的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. 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);
}
}
}
不幸的是,當遠程執行的腳本回復網絡路徑:(很失望......真的很抱歉!只要執行是從遠程系統外的用戶發生的,它就會回覆與該實例相關的絕對路徑。我認爲一個內部進程/用戶應該執行該文件並回答應用程序的答案。
我會盡力想明天更多的東西,也許回信,如果我很幸運。
你爲什麼想這樣做? –
我想這應該給你一個答案? https://superuser.com/questions/1146811/getting-real-path-of-file-on-samba-server-from-client – mariozski
我不明白你在問什麼。或者,也許你沒有。有任何數量的UNC路徑可以引用服務器上的同一文件,包括通過預定義的 $'共享名的文件。是什麼使這些「真正」的路徑?如果某人配置了他們的服務器來禁用''共享?請解決您的問題,以便清楚您在所有情況下實際發生的情況以及原因。你不可能完成你的目標,但目前尚不清楚實際目標是什麼。 –