Parcourir la source

Session.AddDirectorySeparator, Session.GetDirectoryName and Session.GetFileName methods

Source commit: f6363a9d42184ed4807398157252f2e8f01746a8
Martin Prikryl il y a 8 ans
Parent
commit
b1fcb9a110
1 fichiers modifiés avec 74 ajouts et 0 suppressions
  1. 74 0
      dotnet/Session.cs

+ 74 - 0
dotnet/Session.cs

@@ -1224,6 +1224,80 @@ namespace WinSCP
             return result;
         }
 
+        public string GetDirectoryName(string path)
+        {
+            string result;
+            if (path == null)
+            {
+                result = null;
+            }
+            else if (path.Length == 0)
+            {
+                throw Logger.WriteException(new ArgumentException("Path cannot be empty", "path"));
+            }
+            else
+            {
+                int i = path.LastIndexOf('/');
+                if (i < 0)
+                {
+                    result = null;
+                }
+                else if (i == 0)
+                {
+                    if (path.Length == 1)
+                    {
+                        result = null;
+                    }
+                    else
+                    {
+                        result = "/";
+                    }
+                }
+                else
+                {
+                    result = path.Substring(0, i);
+                }
+            }
+            return result;
+        }
+
+        public string AddDirectorySeparator(string path)
+        {
+            if (string.IsNullOrEmpty(path))
+            {
+                throw Logger.WriteException(new ArgumentException("Path cannot be empty", "path"));
+            }
+
+            if (!path.EndsWith("/", StringComparison.Ordinal))
+            {
+                path += "/";
+            }
+
+            return path;
+        }
+
+        public string GetFileName(string path)
+        {
+            string result;
+            if (string.IsNullOrEmpty(path))
+            {
+                result = null;
+            }
+            else
+            {
+                int i = path.LastIndexOf('/');
+                if (i >= 0)
+                {
+                    result = path.Substring(i + 1);
+                }
+                else
+                {
+                    result = string.Empty;
+                }
+            }
+            return result;
+        }
+
         public void AddRawConfiguration(string setting, string value)
         {
             CheckNotOpened();