Friday, May 18, 2012

C#,ASP.NET(Simple Method To Check If File Exists)


public static class ImageType
{
    #region ImageTypes
    public const string Image498x331 = "ImageName.jpg";
    public const string Image196x255 = "ImageName.jpg";
    #endregion ImageTypes
}


public static class clsImage
{
    public const string imgPath = @"ImagePath/here";

    public static string isImageExists(string checkFile, string ImageType)
    {
        #region ImageExists
        try
        {
            if ((checkFile.Trim() != "") && (checkFile.Trim() != string.Empty))
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(checkFile)))
                {
                    return checkFile;
                }
                else
                {
                    return getalternatePath(ImageType);
                }
            }
            else
            {
                return getalternatePath(ImageType);
            }
        }
        catch (Exception ex)
        {
            ErrLog.WriteLog(ex.Message, "isImageExists", "checkFile=" + checkFile + "ImageType=" + ImageType , "clsGeneral.cs");
            return getalternatePath(ImageType);
        }
        #endregion
    }

    private static string getalternatePath(string ImageType)
    {
        #region AlternatePath

        string AlternatePath = imgPath + "notFound/" + ImageType;
        if (!File.Exists(HttpContext.Current.Server.MapPath(AlternatePath)))
        {
            AlternatePath = "Not Available";
        }
       
        return AlternatePath; 
  
        #endregion
    }
}

No comments:

Post a Comment