日期:2014-05-18  浏览次数:21251 次

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

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

如何得到文件名050002.jpg和扩展名JPG

------解决方案--------------------
SubString()方法了,MSDN上查查详细用法
------解决方案--------------------
用\分隔后,再用.分隔。就OK了。
如果是从OpenFileDialog里得到的就更简单了。

------解决方案--------------------
Path.GetExtensition()方法可获得扩展名
至于获得文件名,用字符串截取就OK了
------解决方案--------------------
SubString()可以
------解决方案--------------------
刚才打错了

string fullPath="f:\study\images\050002.jpg ";
int index=fullPath.LastIndexOf("\");
string fileName=fullPath.SubString(index+1);//文件名
string fileExtensition=System.IO.Path.GetExtension(fullPath);//扩展名
------解决方案--------------------
string strFile = @"c:\abc\e.jpg";
string strEx = System.IO.Path.GetExtension(strFile); // 返回 .jpg
string strName = System.IO.Path.GetFileName(strFile); //返回 e.jpg
------解决方案--------------------
探讨
刚才打错了

string fullPath=@"f:\study\images\050002.jpg ";
int index=fullPath.LastIndexOf("\");
string fileName=fullPath.SubString(index+1);//文件名
string fileExtensition=System.IO.Path.GetExtension(fullPath);//扩展名

------解决方案--------------------
C# code
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;
                }

------解决方案--------------------
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 = "";
}
------解决方案--------------------
string test="C:\\Documents and Settings\\Lenovo User\\桌面\\code.txt";
int i;
String FileName;
i=test.LastIndexOf("\\");
FileName=test.Substring(i + 1);

------解决方案--------------------
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
}
------解决方案--------------------
Path.GetDirectoryName();