首页 > 代码库 > C# OpenFileDialog 的用法

C# OpenFileDialog 的用法

OpenFileDialog openFileDialog = new OpenFileDialog();
//打开的文件选择对话框上的标题
openFileDialog.Title = "请选择文件";
//设置文件类型
openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
//设置默认文件类型显示顺序
openFileDialog.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
openFileDialog.RestoreDirectory = true;
//设置是否允许多选
openFileDialog.Multiselect = false;
//按下确定选择的按钮
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
	//获得文件路径
	string localFilePath = openFileDialog.FileName.ToString();
	//获取文件路径,不带文件名
	//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
	//获取文件名,带后缀名,不带路径
	string fileNameWithSuffix = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
	//去除文件后缀名
	string fileNameWithoutSuffix = fileNameWithSuffix.Substring(0, fileNameWithSuffix.LastIndexOf("."));
	//在文件名前加上时间
	string fileNameWithTime = DateTime.Now.ToString("yyyy-MM-dd ") + fileNameExt;
	//在文件名里加字符
	string newFileName = localFilePath.Insert(1, "dameng");
}
本文固定链接:http://www.itechzero.com/coding/c-cpp-csharp/c-sharp-openfiledialog-usage/,转载请注明出处。