【问题标题】:Using File.Exists to check if a file name exists - always returning false使用 File.Exists 检查文件名是否存在 - 总是返回 false
【发布时间】:2019-10-06 05:48:00
【问题描述】:

文件名存在且 ASP.NET C# 始终返回 false。

我一直在尝试 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";
        }
    }

''''

如果输入的文件名存在于 Uploads 文件夹中,我希望看到一个输出检查 TextBox1 用户输入

【问题讨论】:

  • 查看Server.MapPath

标签: c# asp.net verify


【解决方案1】:

在你的 if 语句中,而不是传递相对路径,让它使用 server.mapth 路径定位路径

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

}

试试这个应该没问题

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 2013-02-22
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多