tags:

views:

215

answers:

5

Hi,

I have a string literal as follows:

string filename = @"C:\myfolder\myfile.jpg";

When I use File.Exists(filename) it works most of the time but sometimes I get an error saying the following file doesn't exist:

C:myfoldermyfile.jpg

Something seems to strip the backslashes out of the filename. This code is sometimes accessed via an ajax request.

Does anyone know why/how this could be happening?

Edit:

Here is a more detailed version of the code.

public class Feeds {

    public static string ftpDir = @"C:\website\Feeds\";

}

public class Feed {

    public static void run(string name) {

        if (!Directory.Exists(Feeds.ftpDir + name)){ 

            Response.Write("Feed doesn't exist '" + Feeds.ftpDir + name + "'"); 

            return; 

        }

        //run feed...

    }

}
+7  A: 

Most likely you didn't use Path.Combine to combine path with file name. You should never concatenate path elements using the + operator.

Morten Mertner
This does not seem helpfull at all...
astander
I would give `Path.Combine` a try, it is exactly meant for this type of operations.
Alex Bagnolini
This sounds like it could fix the problem. I just don't understand how it could work sometimes and not others. I'll try this thanks.
CL4NCY
A: 

The backslash character in C# is the escape character, so you're "escaping" the m character twice. You would need a literal \\ everywhere you want a \ in the string.

For your use case, System.IO.Path.Combine doesn't seem necessary since it's a static path, but when you are building paths from, say, two strings, one with a folder name and the other a filename, it's the best way, and it's platform-independent.

(Edit: since you are using the @ character prefix, any stripping of the \ isn't happening in the compiler.)

richardtallent
he's using literals prefixes with `@` - those don't need or support escaping with backslashes.
Eamon Nerbonne
It is not an escape if you use the **@** sign...
astander
A: 

the backslash is an escape character, so after you need to put 2 in a row to get the actual character to show up in your string. you would need to do "C:\myfolder\myfile.jpg"

SwiftStriker00
sorry im new to the site, dont know how to format it propperly, it should be: C:\\myfolder\\myfile.jpg
SwiftStriker00
It is not an escape if you use the **@** sign...
astander
+2  A: 

Probably you simply missed an @ symbol in front of one of your literals somewhere - then @"C:\mydir\myfile.txt" would work, but "C:\mydir\myfile.txt" would not.

You probably know, but just in case: a string prefixed with an @ isn't escaped as usual (i.e. a \n sequence then really means backslash-lowercase n and not newline. It's handy for including raw newlines and other stuff in source, and particularly handy if you want to represent something with lots of backslashes as content (e.g. regexes).

Eamon Nerbonne
A: 

The Directory.Exists(string) call is returning false, because you are passing it a file path rather than a directory path.

I don't know what's stripping the backslashes on the display, unless it has something to do with AJAX.

Jeffrey L Whitledge