Autor Beitrag
friesi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: Mi 15.11.06 01:43 
Hallo :)

Ich habe nochmal eine Frage.
In einem string steht ein Pfad:

ausblenden C#-Quelltext
1:
string dir = "c:\das\ist\ein\pfad";					


Nun würde ich gerne das letzte Verzeichnis als string haben (also pfad).

Könnte mir jemand einen tipp geben, wie ich bis zum letzten "\" den String ermittel?


Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Mi 15.11.2006 um 01:43
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: Mi 15.11.06 01:53 
Hat sich erledigt :)

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
string dir = "c:\das\ist\ein\pfad";

char[] splitter = {'\\'};

string[] dirs = dir.Split(splitter);
string dirname = dirs[dirs.Length-2];
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mi 15.11.06 10:14 
Auch wenn Du selbst einen Weg gefunden hast - der folgende dürfte besser sein:
ausblenden C#-Quelltext
1:
2:
int i0 = dir.LastIndexOf('\');
dirname = dir.SubString(i0);

Gruß Jürgen
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 15.11.06 19:57 
Nicht ganz, so sollte es schon eher hinpassen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
static string ExtractDirectoryName(string path)
{
  char[] separators = new char[] {
    Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar
  };
  
  int stop = path.LastIndexOfAny(separators);
  if (stop == -1)
    return null;
  
  return Path.GetFileName(path.Substring(0, stop));
}


Zuletzt bearbeitet von Kha am Do 16.11.06 14:04, insgesamt 2-mal bearbeitet
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: Mi 15.11.06 21:01 
Danke für die ganzen weiteren (optimierten) Möglichkeiten.
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Do 16.11.06 09:17 
Also bevor ich das Ganze selbst zerpflücke, mache ich das lieber so:
ausblenden C#-Quelltext
1:
2:
3:
4:
string path = @"c:\windows\System32\";
string dirname;
path = path.Trim('\\');
dirname = System.IO.Path.GetFileName(path);
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: Do 16.11.06 09:56 
Warum schreibt man eigentlich immer ein @ vor den Pfad?

ausblenden C#-Quelltext
1:
string path = @"c:\windows\System32\";					


Hab ich jetzt schon öfters gesehen!
MagicAndre1981
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 16.11.06 11:39 
weil man sonst ein "\\" im Pfadnamen eingeben muss, da "\" ein Steuerzeichen ist.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 16.11.06 14:00 
user profile iconUGrohne hat folgendes geschrieben:
Also bevor ich das Ganze selbst zerpflücke, mache ich das lieber so:
Nicht ganz das Geforderte, du müsstest Trim durch LastIndexOf ersetzen. Wenn du die Funktion nun noch plattformunabhängig gestaltest, sind wir beim gleichen Code angelangt, weil ich nun dafür GetFilename übernommen habe ;) .

@friesi: Unbekannte Operatoren oder Funktionen einfach mal in den SDK-Index eintippen.