C#如何截取文件名。。。。。。

Snoworld 2008-04-10 05:25:26
C#如何截取文件名。。。。。。

如:
f:\study\images\050002.jpg

如何得到文件名050002.jpg和扩展名JPG
...全文
1778 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
biganchor 2012-02-04
  • 打赏
  • 举报
回复
还有一个System.IO.Path.GetFileNameWithOutExtension();
cl250 2008-06-15
  • 打赏
  • 举报
回复
FileInfo类
ideacao 2008-04-12
  • 打赏
  • 举报
回复
string[] str = openFileDialog.FileName.Split('\\');
string filename = str[str.Length-1].ToString();
光辉aaaa 2008-04-12
  • 打赏
  • 举报
回复
用string函数截取,再分割就行了。
yudeli 2008-04-11
  • 打赏
  • 举报
回复
hustcyb 2008-04-10
  • 打赏
  • 举报
回复
using System.IO;

string fileName = @"f:\study\images\050002.jpg";
FileInfo fi = new FileInfo(fileName);
string name = fi.Name;
string extension = fi.Extension;
春天的气息 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 apollolb2005 的回复:]
string strFile = @"c:\abc\e.jpg";
string strEx = System.IO.Path.GetExtension(strFile); // 返回 .jpg
string strName = System.IO.Path.GetFileName(strFile); //返回 e.jpg
[/Quote]

顶!
gnimgnot 2008-04-10
  • 打赏
  • 举报
回复
Path.GetDirectoryName();
//取得字符串的路径(即,只有目录名,不要文件名)
//openFileDialogMapLayer.SafeFileNames[i].Remove(openFileDialogMapLayer.SafeFileNames[i].IndexOf("."))
//去掉文件的扩展名

Path.GetFileNameWithoutExtension
RiverCai 2008-04-10
  • 打赏
  • 举报
回复
Using System.IO;

string GetFileInfo(string filePath)
{
string strEx = Path.GetExtension(filePath); // get file extension
string strName = Path.GetFileName(filePath); //get file name
return (strEx+strName); // return file extension & name
}
eagle_2008 2008-04-10
  • 打赏
  • 举报
回复
同意9楼的方法
bianchen 2008-04-10
  • 打赏
  • 举报
回复
string test="C:\\Documents and Settings\\Lenovo User\\桌面\\code.txt";
int i;
String FileName;
i=test.LastIndexOf("\\");
FileName=test.Substring(i + 1);
tanxiaolin520 2008-04-10
  • 打赏
  • 举报
回复
openFileDialog1.InitialDirectory=("C:\\Documents and Settings\\Administrator\\桌面");
openFileDialog1.Filter = "Excel(*.xls)|*.xls";
DialogResult r = openFileDialog1.ShowDialog();
string filepath = openFileDialog1.FileName;
if (r == DialogResult.OK)
{
string y = openFileDialog1.FileName;
label5.Text = y;
button2.Visible = true;
}
else
{
label5.Text = "";
}
gt20083 2008-04-10
  • 打赏
  • 举报
回复
收藏
yuanmanguo 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 apollolb2005 的回复:]
string strFile = @"c:\abc\e.jpg";
string strEx = System.IO.Path.GetExtension(strFile); // 返回 .jpg
string strName = System.IO.Path.GetFileName(strFile); //返回 e.jpg
[/Quote]
蝶恋花雨 2008-04-10
  • 打赏
  • 举报
回复
string  imgname = FileUpload1.PostedFile.FileName;
string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
string quanname =DateTime.Now.ToString("yyyyMMddHHmmss")+imgname.LastIndexOf("\\")+"."+imgType ;
if ("gif" != imgType && "jpg" != imgType && "GIF" != imgType && "JPG" != imgType)
{
Response.Write("<script>alert('请选择gif,jpg格式的文件!');</script>");
return;
}
wudichong 2008-04-10
  • 打赏
  • 举报
回复
狂赞同9楼
hanxiao1999 2008-04-10
  • 打赏
  • 举报
回复
不错!
Magic_YJL 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Magic_YJL 的回复:]
刚才打错了

string fullPath=@"f:\study\images\050002.jpg ";
int index=fullPath.LastIndexOf("\");
string fileName=fullPath.SubString(index+1);//文件名
string fileExtensition=System.IO.Path.GetExtension(fullPath);//扩展名
[/Quote]
red_fish 2008-04-10
  • 打赏
  • 举报
回复
真服了。自己去查一下吧。split的用法。
taiyangyu119 2008-04-10
  • 打赏
  • 举报
回复
严重同意楼上
加载更多回复(9)

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧