日期:2014-05-17 浏览次数:20973 次
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result);
// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''
string s=@"C:\Users\W&C\Desktop\1\2";
string[] dirName = s.Split('\\');
string p = dirName[dirName.Length - 1];
MessageBox.Show(p);//取得2
如果2是1目录下的目录:
string s = @"C:\Users\W&C\Desktop\1\";
string[] dirs = Directory.GetDirectories(s);
System.Collections.ArrayList dirlist = new System.Collections.ArrayList();/*用来保存目录的集合*/
foreach (string item in dirs)
{
dirlist.Add(Path.GetFileNameWithoutExtension(item));//遍历每个目录(不含路径)並加入dirlist集合中
}
foreach (var item in dirlist)
{
//显示目录(不含路径)
MessageBox.Show(item.ToString());
}