SaiyanGirl
SaiyanGirl

Reputation: 17034

Programmatically get the full path of a shared folder on another computer?

I know that you can get the path to a mapped drive (e.g. Find UNC path of a network drive?), but what if the only thing I have is just the path to the shared folder?

For example, let's say I have a friend who is sharing the folder C:\MyDocs\PublicDoc over the network. I can access it under the path \\danas-pc\PublicDoc. Is there any way that I could, being on another computer, determine that \\danas-pc\PublicDoc actually maps to \\danas-pc\c$\MyDocs\PublicDoc?

I ask because I am given a path to a log file that has the path (e.g. \danas-pc\c$\MyDocs\PublicDoc\mylog.log )and I need to check if it matches the same path that is set in another location. The other location has the "short path" (e.g. \\danas-pc\PublicDoc\mylog.log ), and thus, even though the log paths lead to the same location, the program determines that they are different. I wanted to see if there's a way to figure out that they are pointing to the same location.

Upvotes: 3

Views: 6958

Answers (1)

oetoni
oetoni

Reputation: 3907

I can't imagine why you might need this since for the remote instance's full path is \danas-pc\PublicDoc but if you let your imagination thrive I'd suggest something like this:

(1) on the remote computer inside the share folder you can drop a small script that if executed return the full path. You have to search for appropriate coding for windows or linux environment also you need to have execution privilege or rights on it. for example on windows you can have a vbscrit or cscript and a .sh script in linux.

Also please note that seeing it from the remote host, in terms of the remote host the full path is \NAME-OR-IP\Path\to\Folder\or\File etc. For you on the remote connection that is the full path ;)


UPDATE: as per the comment below, this is a full script that does the following

  1. creates a vbscript with code in it to retrieve the current full path
  2. copies the files into the network desired path
  3. executes the vbscript and reads the result back
  4. deletes the vbscript

Assuming: you have the read/write access on the network folder

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

Unfortunately when executed remotely the script replies with the Network Path :( so disappointed...really sorry! As long as execution is happening from a user outside the remote system it will reply with the absolute path related to that instance. I think an internal process/user should execute the file and reply back with the answer to the application.

enter image description here

I'll try to think something more tomorrow and maybe reply back if I'm lucky.

Upvotes: 5

Related Questions