谁知道C#的文件打开对话框怎么才能只显示打开的文件名,而不显示文件路径
OpenFileDialog a = new OpenFileDialog();
a.ShowDialog();
listBox1.Items.Add(a.FileName);
这代码打开后listBox1显示的都是有路径的,如D:\music\不怕不怕.mp3,我要不显示路径,也不要扩展名,求代码
C#文件打开对话框显示文件名问题
答案:2 悬赏:70
解决时间 2021-01-04 13:56
- 提问者网友:空白
- 2021-01-03 22:24
最佳答案
- 二级知识专家网友:duile
- 2021-01-03 23:39
使用path类的方法,里面有很多方法,可以没事看看里面方法的功能。 System.IO.Path.GetFileNameWithoutExtension(a.FileName);
全部回答
- 1楼网友:魅世女王
- 2021-01-04 01:18
public testone()
{
initializecomponent();
savefiledialog();//调用打开savefiledialog 保存对话框
}
#region 保存对话框
private void savefiledialog()
{
//string localfilepath, filenameext, newfilename, filepath;
savefiledialog savefiledialog1 = new savefiledialog();
//设置文件类型
savefiledialog1.filter = " txt files(*.txt)|*.txt|all files(*.*)|*.*";
//设置默认文件类型显示顺序
savefiledialog1.filterindex = 2;
//保存对话框是否记忆上次打开的目录
savefiledialog1.restoredirectory = true;
//点了保存按钮进入
if (savefiledialog1.showdialog() == dialogresult.ok)
{
//获得文件路径
//localfilepath = savefiledialog1.filename.tostring();
//获取文件名,不带路径
//filenameext = localfilepath.substring(localfilepath.lastindexof("\\") + 1);
//获取文件路径,不带文件名
//filepath = localfilepath.substring(0, localfilepath.lastindexof("\\"));
//给文件名前加上时间
//newfilename = datetime.now.tostring("yyyymmdd") + filenameext;
//在文件名里加字符
//savefiledialog1.filename.insert(1,"dameng");
system.io.filestream fs = (system.io.filestream)savefiledialog1.openfile();//输出文件
//fs输出带文字或图片的文件,就看需求了
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯