Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

File exist on server using C#, asp.net

I want to check if a file exists on server's disk and I am using following code

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!");

but I am getting this error:

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("
Log 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:inetpubwwwrootWebStoreJaram Images File doesn't exist!

my log function likes this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So from what I understand, you're getting an "error" because you specifically tell the code to write an error even on success. Try to make your code easier to read. I set up a simple page to test the problem you're having. In the HTML I have:

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

Then the following code is in the CodeBehind. First it checks to make sure if the file exists it sets the URL of the image to the path. If the file doesn't exist, it simply sets the URL of the image to "".

    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
        }
    }

For me, when the file exists, the image displays on the web page. When the file is non-existent, there is no image available. I commented out some other options that might make sense for you as well. The "Picture Not Available.jpg" might be a stock image that you could use to show that an image is not available.

If you're still having problems, make sure to put breakpoints in your code and look at what's actually happening.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...