一、对话框:
1、文件对话框用于选择文件路径和文件名。2、浏览器浏览对话框用于选定文件夹路径。3、保存文件对话框用于指定保存文件路径和文件名。
1、文件对话框:
OpenFileDialog使用:
OpenFileDialog dlgmdb = new OpenFileDialog();
dlgmdb.Filter = "txt文件(*.txt)|*.txt";
dlgmdb.Title = "请选择mdb文件";
if (dlgmdb.ShowDialog() == DialogResult.OK)
{
string file = dlgmdb.FileName;
}
2、FolderBrowserDialog使用:
FolderBrowserDialog fbGDB = new FolderBrowserDialog();
fbGDB.Description = "请选择文件夹";
if (fbGDB.ShowDialog() == DialogResult.OK)
{
txtDCTBPath.Text = fbGDB.SelectedPath;
}
3、SaveFileDialog使用:
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "文本文件(*.txt)|*.txt";//设置文件类型
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//点了保存按钮进入
if (sfd.ShowDialog() == DialogResult.OK)
{
string localFilePath = sfd.FileName; //获得文件路径
}
二、文件路径:
原始路径:C:\Users\hnmt\Desktop\test\output.txt
System.IO.Path.GetFullPath(dlg.FileName);//路径+文件名+扩展名
C:\Users\hnmt\Desktop\test\output.txt
System.IO.Path.GetDirectoryName(dlg.FileName);//路径
C:\Users\hnmt\Desktop\test
System.IO.Path.GetFileName(dlg.FileName);//文件名+扩展名
output.txt
System.IO.Path.GetFileNameWithoutExtension(dlg.FileName);//文件名
output
System.IO.Path.GetExtension(dlg.FileName);//文件扩展名
.txt
-----------------------分割线----------------------------------
原始路径:C:\Users\hnmt\Desktop\test
System.IO.Path.GetFileNameWithoutExtension(dlg.FileName)//最末一个文件夹名称
test
以上函数的返回值都是是string类型。
三、C# vs2010获取程序根目录:
string binPath = AppDomain.CurrentDomain.BaseDirectory;
参:
http://www.cnblogs.com/qnncnblogs/articles/2055036.html
http://www.cnblogs.com/lotusto/p/5767141.html