FileUpload获取文件扩展名

Csh*_*ner 14 c# file-extension file-upload

我正在尝试上传文件并在下面更改其名称.我需要获取文件扩展名.下面的代码在"Path"下面有一个下划线,我错过了一个using语句吗?或者我正在做什么的正确语法是什么?

if (FileUpload1.HasFile)
try
{
    var FileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).Substring(1);                    

    var newName = DateTime.Now.ToLongDateString();
    //Map path to folder
    string realpath = Server.MapPath("Pictures\\") + Guid.NewGuid() + FileExtension;                      

    FileUpload1.SaveAs(realpath);

    Label1.Text = "File name: " +
        FileUpload1.PostedFile.FileName + "<br>" +
        FileUpload1.PostedFile.ContentLength + " kb<br>" +
        "Content type: " +
        FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
    //Handle the error
    throw ex;
}
else
{
    Label1.Text = "You have not specified a file.";
}
Run Code Online (Sandbox Code Playgroud)

jrb*_*jrb 32

FileInfo fi = new FileInfo(fileName);
string ext = fi.Extension;
Run Code Online (Sandbox Code Playgroud)


gif*_*tcv 12

"路径"我错过了使用声明吗?

你必须添加

using System.IO; 
Run Code Online (Sandbox Code Playgroud)

到命名空间列表