views:

7965

answers:

8

Hi,

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#.

Thanks,
Suresh Kumar

+7  A: 

To test whether a file exists in .Net, you can use

System.IO.File.Exists (String)

James Ogden
+2  A: 

Hi,

You could use:

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

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.

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.");
        }
    }
}
splattne
how can i get the selected file path.. ?
What do you mean by "selected path"? There is Server.MapPath(SOME_VIRTUAL_PATH). See here: http://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath-what#275791
splattne
+1  A: 

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.

erikkallen
+2  A: 

Can't comment yet, but I just wanted to disagree/clarify with erikkallen.

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.

Todd Friedlich
I'm absolutely agree with you! But if you look at some frameworks like .Net or Java one tends to believe that it's planned to use exceptions as normal program flow stuff (I don't know how the poor performance fits in that concept!)
rstevens
+1  A: 

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)

ZombieSheep
+1  A: 

You wrote asp.net - are you looking to upload a file?
if so you can use the html

<input type="file" ...

Dror
+3  A: 

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.

Keith