.NET: System.IO.Path

For a line that refers to a directory, how can I make sure that a closing character exists \? For example, C:\foothis is a directory as well C:\foo\. Is there a method System.IO.Paththat provides completion \?

+3
source share
4 answers
if (!filename.EndsWith(Path.DirectorySeparatorChar))
    filename += Path.DirectorySeparatorChar;
+5
source

Presumably you want to add a delimiter so you can add file names later using string concatenation.

: , . , Path.Combine, , -.

, . "D:" - , D:. D:. ? , . :

public static string AppendSeparator(string path)
{
    if (path == null) throw new ArgumentNullException("path");
    if (path.Length == 0) return path;
    if (path[path.Length - 1] == Path.VolumeSeparatorChar) return path;
    if (path[path.Length - 1] == Path.DirectorySeparatorChar) return path;
    if (path[path.Length - 1] == Path.AltDirectorySeparatorChar) return path;
    return path + Path.DirectorySeparatorChar;
}

: :

path = AppendSeparator(@"C:\SomePath\");
path = AppendSeparator(@"C:\SomePath");
path = AppendSeparator(@"D:");
path = AppendSeparator(Path.GetFullPath("D:"));
+3

,

if (s.IndexOf('\\') == s.Length - 1)

s - "amr \" true "amr" false

0
source

Source: https://habr.com/ru/post/1722198/


All Articles