16

I am trying to check if a file is on the server with the C# code behind of my ASP.NET web page. I know the file does exist as I put it on the server in a piece of code before hand. Can anyone see why it is not finding the file. This is the code:

wordDocName = "~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc";
ViewState["wordDocName"] = wordDocName;
if (File.Exists(wordDocName))
{
    btnDownloadWordDoc.Visible = true;
}
else
{
    btnDownloadWordDoc.Visible = false;
}
bluish
  • 26,356
  • 27
  • 122
  • 180
GreeneScreen
  • 643
  • 2
  • 8
  • 15
  • Are you using Unix path conventions on Windows? – Kevin Hsu Jun 01 '11 at 16:58
  • @GreenScreen: why do you convert your ViewState into an int? It will be converted back to a string and you have 2 conversions with the risk of a ConversionException. – slfan Jun 01 '11 at 21:28

8 Answers8

48

the file path should be physical not virtual. Use

if (File.Exists(Server.MapPath(wordDocName)))
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • 3
    Additional info on physical vs virtual: [ASP.NET Web Project Paths](http://msdn.microsoft.com/en-us/library/ms178116.aspx) – MikeM Jun 01 '11 at 17:02
  • The answer, if written for vb.net, needs an additional 3rd, closing parenthesis. I couldn't edit the answer with only one character so I am commenting. – Doreen Jul 26 '16 at 21:13
2

File.Exists() and probably everything else you want to do with the file will need a real Path.

Your wordDocName is a relative URL.

Simply use

string fileName = Server.MapPath(wordDocName);
H H
  • 263,252
  • 30
  • 330
  • 514
1

this might not work if the directory holding the file is referenced by a junction/symbolic link. I have this case in my own application and if I put the REAL path to the file, File.Exists() returns true. But if I use Server.MapPath but the folder is in fact a junction to the folder, it seems to fail. Anyone experienced the same behaviour?

Cristian Cotovan
  • 1,090
  • 1
  • 13
  • 23
1

Use

Server.MapPath("~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc")

to get the fully-qualified path. That should do the trick for ya.

Keith
  • 5,311
  • 3
  • 34
  • 50
1

You need to use Server.MapPath e.g.

    wordDocName = Server.MapPath("~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc");
    ViewState["wordDocName"] = wordDocName;
    if (File.Exists(wordDocName))
    {
        btnDownloadWordDoc.Visible = true;
    }
    else
    {
        btnDownloadWordDoc.Visible = false;
    }
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
0

You have to convert the path to a physical path with Server.MapPath(relativePath)

if (File.Exists(filePath))

wordDocName = "~/specifications/" + ViewState["projectSelected"] + ".doc";        
btnDownloadWordDoc.Visible = File.Exists(Server.MapPath(wordDocName));
slfan
  • 8,950
  • 115
  • 65
  • 78
0

The character "~" is a special char in ASP.NET to get virtual path specifications and simply means "root directory of the application". Is is not understood by the .NET BCL like the File API and must be mapped first into a physical path with Server.MapPath() as others stated.

codymanix
  • 28,510
  • 21
  • 92
  • 151
-3

string docname="traintatkalantnoy.txt";

string a = (Server.MapPath(docname)); if (File.Exists(a))

Kasyx
  • 3,170
  • 21
  • 32