1

I have ran into an issue where by when the user adds a file directory to my project the link is stored as their own mapped drive. For example;

C:\Location\Location

However, some users may have the C: drive on the server mapped as M: for example. Thus are unable to locate the file.

What I would like to do is replace this with the actual server name, ie

\\ArtServer\

I know that I could achieve this by replacing the opening part of the string, however if more servers are added in the future then this will obviously fallover in a huge mess. Currently the user grabs the file path using a standard get file dialogue;

public static string GetFilePath(string filter = "All Files (*.*)|*.*", string initialDirectory = @"This PC")
{
    OpenFileDialog fileDialog = new OpenFileDialog();
    fileDialog.Filter = filter;
    fileDialog.FilterIndex = 1;
    fileDialog.Multiselect = false;

    fileDialog.InitialDirectory = Directory.Exists(initialDirectory) ? initialDirectory : @"This PC";

    if (fileDialog.ShowDialog() == true)
    {
        return fileDialog.FileName;
    }
    else
    {
        return null;
    }
}

Is there anyway I can achieve this with what I currently have?

Shadyjunior
  • 437
  • 3
  • 13
  • What is your purpose? If you simply change `C:\Location\Location` to `\\ArtServer\Location\Location` and wish this to work - you need to be sure that there is `Location` share on your `ArtServer` and that your current user has access to it – vasily.sib Jan 18 '21 at 11:50
  • Also, in some cases `\\ArtServer\C$\Location\Location` might work – vasily.sib Jan 18 '21 at 11:52
  • The main use case for what we have seen is that if someone adds a file directory to the system, a user that does have access to that server path can not open the file because their local system is looking for C: where as the link added to the system maybe M: for example. As a quick fix I have changed it so that when the user adds a ink the first part of the string is replaced with the server name however I do not believe this is great incase more servers are eventually introduced – Shadyjunior Jan 18 '21 at 12:02
  • @ADyson thank you for adding that reference, that is extremely helpful! I apologize for asking this but I noticed an answer from ibram half way down which discusses a use case where by using System.Management which is more prefered as I would like to steer clear of external DLLs as best as I can. However would you know how to implement this using the filedialogue? I hate to ask for code but I am really stuggling to understand how I can implement this into what I need – Shadyjunior Jan 18 '21 at 12:20
  • 1
    I don't know anything about WPF or its dialogs, but assuming you can get the path from the dialog box, then I'd guess you just pass that path into the `GetUNCPath` method from that example, and then use the result in the data you save. – ADyson Jan 18 '21 at 12:30

1 Answers1

1

Thank you to @ADyson for all of your help. I decided to use an answer provided by ibram from the thread linked above. For anyone else who has the same issue I have posted what I had done;

public static string GetUNCPath(string path)
{
    if (path.StartsWith(@"\\"))
    {
        return path;
    }

    ManagementObject mo = new ManagementObject();
    mo.Path = new ManagementPath(String.Format("Win32_LogicalDisk='{0}'", path));

    // DriveType 4 = Network Drive
    if (Convert.ToUInt32(mo["DriveType"]) == 4)
    {
        return Convert.ToString(mo["ProviderName"]);
    }
    else
    {
        return path;
    }
}

public static string GetFilePath(string filter = "All Files (*.*)|*.*", string initialDirectory = @"This PC")
{
    OpenFileDialog fileDialog = new OpenFileDialog();
    fileDialog.Filter = filter;
    fileDialog.FilterIndex = 1;
    fileDialog.Multiselect = false;

    fileDialog.InitialDirectory = Directory.Exists(initialDirectory) ? initialDirectory : @"This PC";

    if (fileDialog.ShowDialog() == true)
    {
        // Split the file directory to gain root path
        // Use GetUNCPath to convert root path to server name
        string s = fileDialog.FileName;
        int index = s.IndexOf(':') + 1;
        string rootPath = GetUNCPath(s.Substring(0, index));
        string directory = s.Substring(index);
        return rootPath + directory;
    }
    else
    {
        return null;
    }
}
Shadyjunior
  • 437
  • 3
  • 13