Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 560 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Verify if file exists or not in C#

#1
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.NET / C#.
Reply

#2
You could use:

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

#3
These answers all assume the file you are checking is on the server side. Unfortunately, there is no cast iron way to ensure that a file exists on the client side (e.g. if you are uploading the resume). Sure, you can do it in Javascript but you are still not going to be 100% sure on the server side.

The best way to handle this, in my opinion, is to assume that the user will actually select an appropriate file for upload, and then do whatever work you need to do to ensure the uploaded file is what you expect (hint - assume the user is trying to poison your system in every possible way with his/her input)
Reply

#4
You wrote asp.net - are you looking to upload a file?<br/>
if so you can use the html

> <input type="file" ...
Reply

#5
Simple answer is that you can't - you won't be able to check a for a file on their machine from an ASP website, as to do so would be a dangerous risk for them.

You have to give them a file upload control - and there's not much you can do with that control. For security reasons javascript can't really touch it.

<asp:FileUpload ID="FileUpload1" runat="server" />

They then pick a file to upload, and you have to deal with any empty file that they might send up server side.
Reply

#6
Can't comment yet, but I just wanted to disagree/clarify with [erikkallen][1].

You should not just catch the exception in the situation you've described. If you KNEW that the file should be there and due to some *exceptional* case, it wasn't, then it would be acceptable to just attempt to access the file and catch any exception that occurs.

In this case, however, you are receiving input from a user and have little reason to believe that the file exists. Here you should always use File.Exists().

I know it is cliché, but you should only use Exceptions for an exceptional event, not as part as the normal flow of your application. It is expensive and makes code more difficult to read/follow.


[1]:

[To see links please register here]

Reply

#7
You can determine whether a specified file exists using the `Exists` method of the `File` class in the `System.IO` namespace:

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

You can find the [documentation here on MSDN][1].

**Example:**

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.");
}
}
}



[1]:

[To see links please register here]

Reply

#8
To test whether a file exists in .NET, you can use

System.IO.File.Exists (String)
Reply

#9
In addition to using `File.Exists()`, you might be better off just trying to use the file and catching any exception that is thrown. The file can fail to open because of other things than not existing.
Reply

#10
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; }
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through