2017-09-10 13 views

Antwort

1

Sie gehen davon aus, dass Ihre E:\folder1 lokalen Pfad als \\mypc\folder1 freigegeben ist, was im Allgemeinen nicht wahr ist, also bezweifle ich eine allgemeine Methode, die tut, was Sie tun möchten, existiert.

Sie sind auf dem richtigen Weg bei der Umsetzung, was Sie erreichen wollen. Sie können weitere Hilfe von System.IO.Path erhalten; siehe Path.GetPathRoot auf MSDN für zurückgegebene Werte entsprechend verschiedene Art von Pfad im Eingang

string GetNetworkPath(string path) 
{ 
    string root = Path.GetPathRoot(path); 

    // validate input, in your case you are expecting a path starting with a root of type "E:\" 
    // see Path.GetPathRoot on MSDN for returned values 
    if (string.IsNullOrWhiteSpace(root) || !root.Contains(":")) 
    { 
     // handle invalid input the way you prefer 
     // I would throw! 
     throw new ApplicationException("be gentle, pass to this function the expected kind of path!"); 
    } 
    path = path.Remove(0, root.Length); 
    return Path.Combine(@"\\myPc", path); 
} 
+0

Schön, aber unsere Lösung wird nur für Microsoft Windows funktionieren. Das Muster für Mac OS Netzwerkpfad könnte anders sein als das von Windows und unsere Lösung wird dort nicht funktionieren. Gibt es also eine konkrete Lösung von Microsoft, die auf allen möglichen Betriebssystemen funktioniert? – rrc1709

Verwandte Themen