C#获取文件名

meceky 2009-03-18 09:20:20
C:\My Document\path\image.jpg
像这样上传文件的时候得到的文件名的文件的绝对路径
我像只获取文件名,像image.jpg,
用C#怎么写啊!
...全文
1310 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyberspaces 2009-11-27
  • 打赏
  • 举报
回复
File.GetFileName("c:/test/win")可以获取目录win的名称
zmxy2005 2009-11-11
  • 打赏
  • 举报
回复
在c#\为转义字符要表示路径中的"\"时候要用"\\"
如 string path = "c:\\book\\c#\\image.jpg";
或者string path = @"c:\book\c#\image.jpg";
zhxhdean 2009-03-18
  • 打赏
  • 举报
回复
using System.IO
string path = "c:\\book\\c#\\image.jpg";
Response.Write(Path.GetFileName(path));
kart15 2009-03-18
  • 打赏
  • 举报
回复
错了,不是"."是"\"
gaoweipeng7851 2009-03-18
  • 打赏
  • 举报
回复
引用命名空间System.IO
string FileName= Path.GetFileName("C:\My Document\path\image.jpg");
kart15 2009-03-18
  • 打赏
  • 举报
回复
string nam="你的路径";
int i = nam.LastIndexOf(".");
string extension = nam.Substring(i);这个就是文件名
kart15 2009-03-18
  • 打赏
  • 举报
回复
string name = FileUpload1.FileName.ToString();
liujiayu10 2009-03-18
  • 打赏
  • 举报
回复
string path = Server.MapPath("upload/");
string filename = File1.PostedFile.FileName;
string ftype = File1.PostedFile.ContentType;
int filesize = File1.PostedFile.ContentLength;
string sOriginalFileName = System.IO.Path.GetFileName(filename);//这个是你想要的
string sFileExt = System.IO.Path.GetExtension(sOriginalFileName);
  • 打赏
  • 举报
回复
string path = Path.GetFileName("C:\My Document\path\image.jpg");
daniel402 2009-03-18
  • 打赏
  • 举报
回复
string fileName = filePath.Substring(filePath.LastIndexOf(@"\")+1);
  • 打赏
  • 举报
回复
string path = Path.GetFileName("C:\My Document\path\image.jpg");

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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