Verify if file exists or not in C#

后端 未结 12 1341
名媛妹妹
名媛妹妹 2020-12-15 17:19

I am working on an application. That application should get the resume from the users, so that I need a code to verify whether a file exists or not.

I\'m using ASP.N

12条回答
  •  独厮守ぢ
    2020-12-15 17:50

    This may help you.

    try
       {
           con.Open();
           if ((fileUpload1.PostedFile != null) && (fileUpload1.PostedFile.ContentLength > 0))
           {
               filename = System.IO.Path.GetFileName(fileUpload1.PostedFile.FileName);
               ext = System.IO.Path.GetExtension(filename).ToLower();
               string str=@"/Resumes/" + filename;
               saveloc = (Server.MapPath(".") + str);
               string[] exts = { ".doc", ".docx", ".pdf", ".rtf" };
               for (int i = 0; i < exts.Length; i++)
               {
                   if (ext == exts[i])
                       fileok = true;
               }
               if (fileok)
               {
                   if (File.Exists(saveloc))
                       throw new Exception(Label1.Text="File exists!!!");
                   fileUpload1.PostedFile.SaveAs(saveloc);
                   cmd = new SqlCommand("insert into candidate values('" + candidatename + "','" + candidatemail + "','" + candidatemobile + "','" + filename + "','" + str + "')", con);
                   cmd.ExecuteNonQuery();
                   Label1.Text = "Upload Successful!!!";
                   Label1.ForeColor = System.Drawing.Color.Blue;
                   con.Close();
               }
               else
               {
                   Label1.Text = "Upload not successful!!!";
                   Label1.ForeColor = System.Drawing.Color.Red;
               }
           }
    
        }
       catch (Exception ee) { Label1.Text = ee.Message; }
    

提交回复
热议问题