11.8 Einen Pfad parsen

Problem

Sie müssen die Bestandteile einer Pfadangabe trennen und in separaten Variablen speichern.

Lösung

Nutzen Sie die statischen Methoden der Klasse Path:

public static void ParsePath(string path) { string root = Path.GetPathRoot(path); string dirName = Path.GetDirectoryName(path); string fullFileName = Path.GetFileName(path); string fileExt = Path.GetExtension(path); string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path); StringBuilder format = new StringBuilder( ); format.Append("Pfad von {0} besteht aus den folgenden Bestandteilen:" + Environment.NewLine + "\tWurzel: {1}" + Environment.NewLine + "\t"); format.Append("Verzeichnisname: {2}" + Environment.NewLine + "\tVollständiger Dateiname: {3}" + Environment.NewLine ...

Get C# Kochbuch now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.