Get network path

2

I have a Form that is saved on the network that is mapped as \\BRJGS090\suporte$ on the Z: drive. This Form runs an application that is in the same folder through ProcessStartInfo , on the local computer it works normally but when I run it from the network it says that the application can not be found.

I've tried to use Application.StartupPath , AppDomain.CurrentDomain.BaseDirectory , but I've checked that it returns the path as Z:\programas\aplicativos , which is where the Form and the other application are. Is there any way to get this network path as \\BRJGSD090\suporte$\programas\aplicativos ?

    
asked by anonymous 29.10.2014 / 01:29

2 answers

1

There is no native function in the current implementation of the framework that transforms a path into a directory mapped to your network URI. You can, however, implement a function:

public static class Pathing
{
    [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int WNetGetConnection(
        [MarshalAs(UnmanagedType.LPTStr)] string localName, 
        [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, 
        ref int length);
    /// <summary>
    /// Given a path, returns the UNC path or the original. (No exceptions
    /// are raised by this function directly). For example, "P:08-02-29"
    /// might return: "\networkserver\Shares\Photos08-02-09"
    /// </summary>
    /// <param name="originalPath">The path to convert to a UNC Path</param>
    /// <returns>A UNC path. If a network drive letter is specified, the
    /// drive letter is converted to a UNC or network path. If the 
    /// originalPath cannot be converted, it is returned unchanged.</returns>
    public static string GetUNCPath(string originalPath)
    {
        StringBuilder sb = new StringBuilder(512);
        int size = sb.Capacity;

        // look for the {LETTER}: combination ...
        if (originalPath.Length > 2 && originalPath[1] == ':')
        {
            // don't use char.IsLetter here - as that can be misleading
            // the only valid drive letters are a-z && A-Z.
            char c = originalPath[0];
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            {
                int error = WNetGetConnection(originalPath.Substring(0, 2), 
                    sb, ref size);
                if (error == 0)
                {                        
                    DirectoryInfo dir = new DirectoryInfo(originalPath);

                    string path = Path.GetFullPath(originalPath)
                        .Substring(Path.GetPathRoot(originalPath).Length);
                    return Path.Combine(sb.ToString().TrimEnd(), path);
                }
            }
        }
        return originalPath;
    }
}

In your example, GetUNCPath("Z:\programas\aplicativos") will return the string "\BRJGSD090\suporte$\programas\aplicativos" .

External source: Converting a mapped drive to a network path using C #

    
24.02.2016 / 14:37
0

You can save the address in a variable in the database or app.config by telling which directory you want to work with and save that address there.

If necessary, you can even create a screen for the user or administrator to change that path.

    
16.03.2015 / 13:50