OpenFileDialog openFileDialog = new OpenFileDialog();
//打开的文件选择对话框上的标题
openFileDialog.Title = "请选择文件";
//设置文件类型
openFileDialog.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*";
//设置默认文件类型显示顺序
openFileDialog.FilterIndex = 1;
//保存对话框是否记忆上次打开的文件夹
openFileDialog.RestoreDirectory = true;
//设置是否同意多选
openFileDialog.Multiselect = false;
//按下确定选择的button
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/c-sharp-openfiledialog-usage.html。转载请注明出处。