【问题标题】:File exist on server using C#, asp.net文件存在于使用 C#、asp.net 的服务器上
【发布时间】:2012-05-12 17:26:42
【问题描述】:

我想检查服务器磁盘上是否存在文件,我正在使用以下代码

if (File.Exists(Server.MapPath("~/Jaram Images/") + Path.GetFileName(product.Pic_Url2)))
                                            {
                                                WriteError("File  exist!");

                                                //PdfProdCell = new PdfPCell(iTextSharp.text.Image.GetInstance(Server.MapPath("~/Jaram Images/") + Path.GetFileName(product.Pic_Url2)), true);
                                            }
                                            else
                                                WriteError(Server.MapPath("~/Jaram Images/") + " File doesn't exist!");

但我收到此错误:

public static void WriteError(string errorMessage)
{
    try
    {
        string path = "~/Jaram PDF/PDFS/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
        }
        using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            w.WriteLine("\r\nLog Entry : ");
            w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
            string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                          ". Error Message:" + errorMessage;
            w.WriteLine(err);
            w.WriteLine("__________________________");
            w.Flush();
            w.Close();
        }
    }
    catch (Exception ex)
    {
        WriteError(ex.Message);
    }

}

Log Entry : 
05/03/2012 15:50:51
Error in: http://localhost/WebStore/AdminNewAccount.aspx?role=+Administrator. Error Message:C:\inetpub\wwwroot\WebStore\Jaram Images\ File doesn't exist!

我的日志功能喜欢这样

【问题讨论】:

  • 文件是否存在于Server.MapPath("~/Jaram Images/") + Path.GetFileName(product.Pic_Url2)) ...?
  • 首先,这不是您从异常中得到的实际错误。其次,您映射同一个文件 3 次,为什么不第一次映射它。此外,不存在的文件实际上是您键入时的目录。最后,调试你的代码,看看真正的问题是什么
  • 不确定您遇到的具体问题是什么 - 您那里有很多代码,但错误消息很少。作为一个随机评论,您可能想要考虑使用真正的日志库而不是自己滚动。我建议NLog 或(在较小程度上)log4net。它们超级易于使用、超级可配置,并且比您自己编写的任何代码都具有更高的容错性(除非您对日志代码进行大量工作)。跨度>
  • 是的,该文件不存在。当文件不存在时,我想避免此错误,这就是我应用 If 条件的原因。
  • @All 如何检查文件是否存在,以便我可以显示图像,否则,跳过它?

标签: c# asp.net


【解决方案1】:

因此,据我了解,您会收到“错误”,因为您明确告诉代码即使在成功时也要编写错误。尝试使您的代码更易于阅读。我设置了一个简单的页面来测试您遇到的问题。在我的 HTML 中:

<body>
<form id="form1" runat="server">
<div>
    <asp:Image runat="server" ID="TestPicture" />
</div>
</form>
</body>

那么下面的代码在CodeBehind中。首先,它检查以确保文件是否存在,它将图像的 URL 设置为路径。如果文件不存在,它只是将图像的 URL 设置为 ""。

    protected void Page_Load(object sender, EventArgs e)
    {
        string serverPath = Server.MapPath("~/Test/") + Path.GetFileName("~/Test/TestImg.jpg");
        string imgUrl = "~/Test/TestImg.jpg";
        if (File.Exists(serverPath))
        {
            TestPicture.ImageUrl = imgUrl;
        }
        else
        {
            TestPicture.ImageUrl = "";
            //TestPicture.Visible = false;
            //TestPicture.ImageUrl = "Picture Not Available.jpg";

            //or do other error checking here
        }
    }

对我来说,当文件存在时,图像会显示在网页上。当文件不存在时,没有可用的图像。我还注释了一些其他可能对您有意义的选项。 “Picture Not Available.jpg”可能是一张库存图片,您可以使用它来显示图片不可用。

如果您仍然遇到问题,请确保在代码中放置断点并查看实际发生的情况。

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多