views:

619

answers:

6

I get a string that more or less looks like this:

"C:\\bláh\\bleh"

I make a FileInfo with it, but when I check for its existence it returns false:

var file = new FileInfo(path);
file.Exists;

If I manually rename the path to

"C:\\blah\\bleh"

at debug time and ensure that blah exists with a bleh inside it, then file.Exists starts returning true. So I believe the problem is the non-ascii character.

The actual string is built by my program. One part comes from the AppDomain of the application, which is the part that contains the "á", the other part comes, in a way, from the user. Both parts are put together by Path.Combine. I confirmed the validity of the resulting string in two ways: copying it from the error my program generates, which includes the path, into explorer opens the file just fine. Looking at that string at the debugger, it looks correctly escaped, in that \ are written as \. The "á" is printed literarily by the debugger.

How should I process a string so that even if it has non-ascii characters it turns out to be a valid path?

A: 

Use System.IO.Path.GetInvalidFileNameChars() method to get array of invalid character of filename.

  if (filename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars())==-1) // valid path
    {
         ....
    }
adatapost
This method (GetInvalidFileNameChars) will not return all invalid chars for path (read this http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx). And this is not problem in this case, as the 'á' character is legal to be used in path.
Kamarey
The path I have is valid, pasting in on explorer shows the correct file.
J. Pablo Fernández
A: 

try this

    string sourceFile = @"C:\bláh\bleh";
    if (File.Exists(sourceFile))
    {

         Console.WriteLine("file exist.");

    }
    else
    {
        Console.WriteLine("file does not exist.");

    }

Note : The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Exists returns false.

For path validation you can use Directory.Exists.

RRUZ
I'm not trying to validate the path, I want to be sure the file actually exists.
J. Pablo Fernández
A: 

I have just manuall created a bláh folder containing a bleh file, and with that in place, this code prints True as expected:

using System;
using System.IO;

namespace ConsoleApplication72
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "c:\\bláh\\bleh";

            FileInfo fi = new FileInfo(filename);

            Console.WriteLine(fi.Exists);

            Console.ReadLine();
        }
    }
}

I would suggest checking the source of your string - in particular, although your 3k rep speaks against this being the problem, remember that expressing a backslash as \\ is an artifact of C# syntax, and you want to make sure your string actually contains only single \s.

AakashM
The string comes from somewhere else and I just add more stuff to it with Path.Combine. When I print the string it looks like c:\bláh\bleh, which I can copy and paste in the file path text box of the explorer and it opens the file. And when I look at it in the debugger I see c:\\bláh\\bleh, so I believe the string is actually correct. Latter on I will try doing it in a console app like you did and see what happens.
J. Pablo Fernández
A: 

Referring to @adatapost's reply, the list of invalid file name characters (gleaned from System.IO.Path.GetInvalidFileNameChars() in fact doesn't contain normal characters with diacritics.

It looks like the question you're really asking is, "How do I remove diacritics from a string (or in this case, file path)?".

Or maybe you aren't asking this question, and you genuinely want to find a file with name:

c:\blòh\bleh

(or something similar). In that case, you then need to try to open a file with the same name, and not c:\bloh\bleh.

Eric Smith
I want to work on the file c:\bláh\bleh, not on c:\blah\bleh. I tried my program with c:\blah\bleh just to verify that the "á" was the problem. If I change the path in any way I'll end up pointing to a file that doesn't exist, because the path is correct (I can copy and paste it in explorer and the correct file gets open).
J. Pablo Fernández
A: 

Look like the "bleh" in the path is a directory, not a file. To check if the folder exist use Directory.Exists method.

Kamarey
No, it is a file.
J. Pablo Fernández
A: 

The problem was: the program didn't have enough permissions to access that file. Fixing the permissions fixed the problem. It seems that when I didn't my experiment I somehow managed to reproduce the permission problem, possibly by creating the folder without the non-ascii character by hand and copying the other one.

Oh... so embarrassing.

J. Pablo Fernández