C# 获取文件名 只要文件名,不要路径,文件类型有没有都行,没有最好
3个回答
2012-05-17
展开全部
用FileInfo获取啊,你如果只需要某个文件的文件名,通过FileInfo的Name即可获取
如果是你要获取的对象是一个目录,则使用directoryinfo的Name属性
如果是你要获取的对象是一个目录,则使用directoryinfo的Name属性
展开全部
file.Name不就是文件名么?
后缀名不要可以去掉:string[] strName = file.Name.Split('.'); string name = strName [0];
后缀名不要可以去掉:string[] strName = file.Name.Split('.'); string name = strName [0];
更多追问追答
追问
请恕我孤陋寡闻,file.name是哪个命名空间下的?
追答
不是。。。file是你的文件 Name是文件的一个属性。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if (this.ImgFileUp.PostedFile.ContentLength > 0)
{
string fullname = this.ImgFileUp.PostedFile.FileName.ToString();
string filename = fullname.Substring(fullname.LastIndexOf("\\") + 1);
filename = filename.Substring(0, filename.LastIndexOf("."));
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1).ToLower();
string currentFileName = "";
if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png" || typ2 == "jpeg")
{
currentFileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "." + typ2;
string newbag = DateTime.Now.ToString("yyyyMMdd") ;
if (!System.IO.Directory.Exists(Server.MapPath("~\\upload_files\\images\\" + newbag)))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~\\upload_files\\images\\" + newbag));
}
this.ImgFileUp.SaveAs(Server.MapPath("~\\upload_files\\images\\" + newbag + "") + "\\" + currentFileName);
this.ImgUrl1.Value = "upload_files/images/" + newbag + "/" + currentFileName;
//Response.Write(this.ImgUrl1.Value);
}
else if (typ2 == "txt" || typ2 == "zip" || typ2 == "rar" || typ2 == "doc" || typ2 == "docx" || typ2 == "xls" || typ2 == "ppt" || typ2 == "pdf" || typ2 == "swf" || typ2 == "flv")
{
currentFileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "." + typ2;
string newsfile = DateTime.Now.ToString("yyyyMMdd") ;
if (!System.IO.Directory.Exists(Server.MapPath("~\\upload_files\\files\\" + newsfile)))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~\\upload_files\\files\\" + newsfile));
}
this.ImgFileUp.SaveAs(Server.MapPath("~\\upload_files\\files\\" + newsfile + "") + "\\" + currentFileName);
this.ImgUrl1.Value = "upload_files/files/" + newsfile + "/" + currentFileName;
//this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert('"+this.ImgUrl1.Value+"');", true);
//Response.Write("<script language='javascript'>parent.Alert('图片必须gif,jpg,bmp或者png格式!');</script>");
//Response.End();
//return;
}
{
string fullname = this.ImgFileUp.PostedFile.FileName.ToString();
string filename = fullname.Substring(fullname.LastIndexOf("\\") + 1);
filename = filename.Substring(0, filename.LastIndexOf("."));
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1).ToLower();
string currentFileName = "";
if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png" || typ2 == "jpeg")
{
currentFileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "." + typ2;
string newbag = DateTime.Now.ToString("yyyyMMdd") ;
if (!System.IO.Directory.Exists(Server.MapPath("~\\upload_files\\images\\" + newbag)))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~\\upload_files\\images\\" + newbag));
}
this.ImgFileUp.SaveAs(Server.MapPath("~\\upload_files\\images\\" + newbag + "") + "\\" + currentFileName);
this.ImgUrl1.Value = "upload_files/images/" + newbag + "/" + currentFileName;
//Response.Write(this.ImgUrl1.Value);
}
else if (typ2 == "txt" || typ2 == "zip" || typ2 == "rar" || typ2 == "doc" || typ2 == "docx" || typ2 == "xls" || typ2 == "ppt" || typ2 == "pdf" || typ2 == "swf" || typ2 == "flv")
{
currentFileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "." + typ2;
string newsfile = DateTime.Now.ToString("yyyyMMdd") ;
if (!System.IO.Directory.Exists(Server.MapPath("~\\upload_files\\files\\" + newsfile)))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~\\upload_files\\files\\" + newsfile));
}
this.ImgFileUp.SaveAs(Server.MapPath("~\\upload_files\\files\\" + newsfile + "") + "\\" + currentFileName);
this.ImgUrl1.Value = "upload_files/files/" + newsfile + "/" + currentFileName;
//this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert('"+this.ImgUrl1.Value+"');", true);
//Response.Write("<script language='javascript'>parent.Alert('图片必须gif,jpg,bmp或者png格式!');</script>");
//Response.End();
//return;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询