Kaynağa Gözat

Added Session.TranslateLocalPathToRemote to generate a remote path equivalent of a local path, given root paths.

Source commit: 8f62f63fa4352891891fff1aac7a2c59602b24d8
Martin Prikryl 9 yıl önce
ebeveyn
işleme
1fe3618a66
1 değiştirilmiş dosya ile 53 ekleme ve 0 silme
  1. 53 0
      dotnet/Session.cs

+ 53 - 0
dotnet/Session.cs

@@ -1027,6 +1027,59 @@ namespace WinSCP
             return localPath;
         }
 
+        public string TranslateLocalPathToRemote(string localPath, string localRoot, string remoteRoot)
+        {
+            if (localPath == null)
+            {
+                throw new ArgumentNullException("localPath");
+            }
+
+            if (localRoot == null)
+            {
+                throw new ArgumentNullException("localRoot");
+            }
+
+            if (remoteRoot == null)
+            {
+                throw new ArgumentNullException("remoteRoot");
+            }
+
+            if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
+            {
+                localRoot += "\\";
+            }
+
+            // not adding to empty root paths, because the path may not even start with slash
+            if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
+            {
+                remoteRoot += "/";
+            }
+
+            string remotePath;
+            // special case
+            if (localPath == localRoot)
+            {
+                remotePath = remoteRoot;
+            }
+            else
+            {
+                if (!localPath.StartsWith(localRoot, StringComparison.Ordinal))
+                {
+                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", localPath, localRoot));
+                }
+
+                string subPath = localPath.Substring(localRoot.Length);
+                // can happen only when localRoot is empty
+                if (subPath.StartsWith("\\", StringComparison.Ordinal))
+                {
+                    subPath = subPath.Substring(1);
+                }
+                subPath = subPath.Replace('\\', '/');
+                remotePath = remoteRoot + subPath;
+            }
+            return remotePath;
+        }
+
         public string CombinePaths(string path1, string path2)
         {
             if (path1 == null)