2

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?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • Where does the string come from; is it coded into a code file or is it provided by the user? – Fredrik Mörk Aug 19 '09 at 06:50
  • Fredrik Mörk, I've just added that information to the question itself. – Pablo Fernandez Aug 19 '09 at 07:53
  • 1
    That's a wierd one. I created a file with the same path and name, and the following code prints `"True"`: `FileInfo fi = new FileInfo("C:\\bláh\\bleh"); Console.WriteLine(fi.Exists);` – Fredrik Mörk Aug 19 '09 at 08:03
  • 1
    What code of 'á' generated by your app? And what code of the this char, if get it from the path (Find all folders on "C:\", find this specific folder and look in debug what the code)? – Kamarey Aug 19 '09 at 08:06

6 Answers6

1

Here is a method that will handle diacritics in filenames. The success of the File.Exists method depends on how your system stores the filename.

public bool FileExists(string sPath)
{
  //Checking for composed and decomposed is to handle diacritics in filenames.  
  var pathComposed = sPath.Normalize(NormalizationForm.FormC);
  if (File.Exists(pathComposed))    
      return true;

   //We really need to check both possibilities.
   var pathDecomposed = sPath.Normalize(NormalizationForm.FormD);
   if (File.Exists(pathDecomposed))     
      return true;

   return false;
}
Rick M
  • 11
  • 1
0

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
  • 134,889
  • 20
  • 356
  • 483
0

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
  • 62,551
  • 17
  • 151
  • 186
  • 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. – Pablo Fernandez Aug 19 '09 at 07:45
0

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.

Community
  • 1
  • 1
Eric Smith
  • 5,262
  • 2
  • 33
  • 49
  • 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). – Pablo Fernandez Aug 19 '09 at 07:47
0

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

Kamarey
  • 10,832
  • 7
  • 57
  • 70
0

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.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622