简体   繁体   English

使用File.Exists检查文件名是否存在 - 始终返回false

[英]Using File.Exists to check if a file name exists - always returning false

File Name exists and ASP.NET C# is always returning false. 文件名存在且ASP.NET C#始终返回false。

I have been trying File.Exists 我一直在尝试File.Exists

protected void Button1_Click(object sender, EventArgs e)
    {
        Server.MapPath("~");
        string location = ("~/Uploads/");
        string filename = TextBox1.Text;
        string currentfile = (location + filename);
        if (System.IO.File.Exists(currentfile))
        {
            Label1.Visible = true;
            Label1.Text = "File Exists in Uploads";
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "File Name Not Found";
        }
    }

'''' “”“”

I hope to see an output that checks with TextBox1 user input if the filename entered exists in the Uploads folder 如果输入的文件名存在于Uploads文件夹中,我希望看到用TextBox1用户输入检查的输出

in your if statement instead of passing the relative path have it locate the path using server.mapth path 在if语句中,而不是传递相对路径,它使用server.mapth路径定位路径

if(System.IO.File.Exists(Server.MapPath(currentfile))){

}

try this it should work without a problem 试试这个它应该没有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM