path process question
I have a file name called a.b.c.d if I use Path.Ge开发者_如何转开发tFileNameWithoutExtension, I get a.b.c, I am wondering how to get a part only
Here's one way:
var path = "asdf.bsdf.csdf";
while(Path.HasExtension(path))
path = Path.GetFileNameWithoutExtension(path);
Here's another:
path = "asdf.bsdf.csdf";
path = path.Substring(0, path.IndexOf('.'));
Note that the latter would require a little modification if path
were actually a path and not just a file name, whereas the first would not.
There are lots of other ways to do this -- these are just two examples.
Option 1:
string p = Path.GFNWE (Path.GFNWE (Path.GFNWE (original)));
Option 2:
string p = original;
int i;
while ((i = p.LastIndexOf ('.')) > 0) {
p = Path.GetFileNameWithoutExtension (p);
}
Option 3: (careful, case-sensitive)
if (original.EndsWith (".b.c.d"))
original = original.Substring (0, original.Length - ".b.c.d".Length);
Well you could try something like
Path.GetFileName("a.b.c.d").Split('.')[0]
string fileName = "a.b.c.d";
string whatYouWant = fileName.Substring(0, fileName.IndexOf('.'));
精彩评论