在C#中validation文件是否存在

我正在开发一个应用程序。 该应用程序应该从用户那里获得简历,因此我需要一个代码来validation文件是否存在。

我正在使用ASP.NET / C#。

您可以使用System.IO命名空间中的File类的Exists方法确定是否存在指定的文件:

 bool System.IO.File.Exists(string path) 

您可以在MSDN上找到此处的文档 。

例:

 using System; using System.IO; class Test { public static void Main() { string resumeFile = @"c:\ResumesArchive\923823.txt"; string newFile = @"c:\ResumesImport\newResume.txt"; if (File.Exists(resumeFile)) { File.Copy(resumeFile, newFile); } else { Console.WriteLine("Resume file does not exist."); } } } 

要测试.NET中是否存在文件,您可以使用

 System.IO.File.Exists (String) 
  if (File.Exists(Server.MapPath("~/Images/associates/" + Html.DisplayFor(modelItem => item.AssociateImage)))) {  } else { 
No image available
}

我做了类似这样的事情,以便在显示之前检查图像是否存在。

简单的答案是你不能 – 你将无法从ASP网站检查他们机器上的文件,因为这样做对他们来说是一个危险的风险。

你必须给他们一个文件上传控件 – 你可以用这个控件做多少。 出于安全考虑,javascript无法真正触及它。

  

然后他们选择要上传的文件,并且您必须处理他们可能在服务器端发送的任何空文件。

你可以使用:

 System.IO.File.Exists(@"c:\temp\test.txt"); 

无法评论,但我只是想与erikkallen不同意/澄清。

你不应该只是在你描述的情况下捕获exception。 如果您知道该文件应该在那里并且由于某些特殊情况而不是,那么只是尝试访问该文件并捕获发生的任何exception是可以接受的。

但是,在这种情况下,您正在接收来自用户的输入,并且没有理由相信该文件存在。 在这里你应该总是使用File.Exists()。

我知道这是陈词滥调,但是你应该只对特殊事件使用Exceptions,而不是像应用程序的正常流程一样。 它很昂贵,使代码更难以阅读/遵循。

这些答案都假设您正在检查的文件位于服务器端。 不幸的是,没有铸铁的方法来确保客户端上存在文件(例如,如果您要上传简历)。 当然,你可以在Javascript中完成它,但你仍然不会在服务器端100%确定。

在我看来,处理此问题的最佳方法是假设用户实际上会选择适当的文件进行上传,然后执行您需要做的任何工作以确保上传的文件符合您的预期(提示 – 假设用户试图用他/她的输入以各种可能的方式毒害你的系统

你写了asp.net – 你想上传文件吗?
如果是这样,你可以使用HTML

除了使用File.Exists() ,您最好只是尝试使用该文件并捕获引发的任何exception。 由于除了不存在之外的其他事情,该文件可能无法打开。

试试这个:

  string fileName = "6d294041-34d1-4c66-a04c-261a6d9aee17.jpeg"; string deletePath= "/images/uploads/"; if (!string.IsNullOrEmpty(fileName )) { // Append the name of the file to previous image. deletePath += fileName ; if (File.Exists(HttpContext.Current.Server.MapPath(deletePath))) { // deletevprevious image File.Delete(HttpContext.Current.Server.MapPath(deletePath)); } } 

这可能对你有所帮助。

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

我已经在vb中编写了这段代码,并且它可以正常工作以检查文件存在与否的天气以进行文件上载控制。 试试吧

对于VB代码============

  If FileUpload1.HasFile = True Then Dim FileExtension As String = System.IO.Path.GetExtension(FileUpload1.FileName) If FileExtension.ToLower <> ".jpg" Then lblMessage.ForeColor = System.Drawing.Color.Red lblMessage.Text = "Please select .jpg image file to upload" Else Dim FileSize As Integer = FileUpload1.PostedFile.ContentLength If FileSize > 1048576 Then lblMessage.ForeColor = System.Drawing.Color.Red lblMessage.Text = "File size (1MB) exceeded" Else Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.FileName) Dim ServerFileName As String = Server.MapPath("~/Images/Folder1/" + FileName) If System.IO.File.Exists(ServerFileName) = False Then FileUpload1.SaveAs(Server.MapPath("~/Images/Folder1/") + FileUpload1.FileName) lblMessage.ForeColor = System.Drawing.Color.Green lblMessage.Text = "File : " + FileUpload1.FileName + " uploaded successfully" Else lblMessage.ForeColor = System.Drawing.Color.Red lblMessage.Text = "File : " + FileName.ToString() + " already exsist" End If End If End If Else lblMessage.ForeColor = System.Drawing.Color.Red lblMessage.Text = "Please select a file to upload" End If 

对于C#CODE ======================

 if (FileUpload1.HasFile == true) { string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName); if (FileExtension.ToLower != ".jpg") { lblMessage.ForeColor = System.Drawing.Color.Red; lblMessage.Text = "Please select .jpg image file to upload"; } else { int FileSize = FileUpload1.PostedFile.ContentLength; if (FileSize > 1048576) { lblMessage.ForeColor = System.Drawing.Color.Red; lblMessage.Text = "File size (1MB) exceeded"; } else { string FileName = System.IO.Path.GetFileName(FileUpload1.FileName); string ServerFileName = Server.MapPath("~/Images/Folder1/" + FileName); if (System.IO.File.Exists(ServerFileName) == false) { FileUpload1.SaveAs(Server.MapPath("~/Images/Folder1/") + FileUpload1.FileName); lblMessage.ForeColor = System.Drawing.Color.Green; lblMessage.Text = "File : " + FileUpload1.FileName + " uploaded successfully"; } else { lblMessage.ForeColor = System.Drawing.Color.Red; lblMessage.Text = "File : " + FileName.ToString() + " already exsist"; } } } } else { lblMessage.ForeColor = System.Drawing.Color.Red; lblMessage.Text = "Please select a file to upload"; }