我不能想象为什么需要这个,因为远程实例的完整路径是\达纳斯-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路径可以引用服务器上的同一文件,包括通过预定义的 $'共享名的文件。是什么使这些“真正”的路径?如果某人配置了他们的服务器来禁用''共享?请解决您的问题,以便清楚您在所有情况下实际发生的情况以及原因。你不可能完成你的目标,但目前尚不清楚实际目标是什么。 –