2017-11-03 221 views
2

我知道你可以获得映射驱动器的路径(例如Find UNC path of a network drive?),但如果我拥有的唯一东西只是共享文件夹的路径呢?以编程方式获取另一台计算机上共享文件夹的完整路径?

例如,假设我有一个朋友在网络上共享文件夹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),因此即使日志路径通向相同的位置,程序也会确定它们不同。我想看看是否有办法弄清楚他们指向的是同一个位置。

+0

你为什么想这样做? –

+0

我想这应该给你一个答案? https://superuser.com/questions/1146811/getting-real-path-of-file-on-samba-server-from-client – mariozski

+0

我不明白你在问什么。或者,也许你没有。有任何数量的UNC路径可以引用服务器上的同一文件,包括通过预定义的 $'共享名的文件。是什么使这些“真正”的路径?如果某人配置了他们的服务器来禁用''共享?请解决您的问题,以便清楚您在所有情况下实际发生的情况以及原因。你不可能完成你的目标,但目前尚不清楚实际目标是什么。 –

回答

3

我不能想象为什么需要这个,因为远程实例的完整路径是\达纳斯-PC \ PublicDoc但如果你让你的想象力蓬勃发展,我建议是这样的:

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

另请注意,从远程主机来看,就远程主机而言,完整路径是\ NAME-OR-IP \ Path \ to \ Folder \或\ File等。对于远程连接是完整路径;)


UPDATE: 按下面的评论,这是做以下

  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. 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); 
     } 
    } 
} 

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

enter image description here

我会尽力想明天更多的东西,也许回信,如果我很幸运。

+0

您可以显示一个C#代码的小例子,它将在路径中写入此文件,执行文件,捕获输出并删除文件?这个想法很好,但它会让答案更加完整。 –

+0

您好Rufus,我根据您的评论做了脚本,但不幸的是它不能用于远程执行。我相信每当执行发生在远程时,路径就被视为这样。主机上的内部进程应该执行脚本并将结果发送回主应用程序供我们阅读 – oetoni

相关问题