1

I have an application that accepts user input and does stuff to files. The users select a file and it might move it, delete it, rename it, ftp it etc. The application uses a hash table to store recently used files and their paths.

The main problem I am looking into now is one of the add-ins is saving the path incorrectly, it is saving it as such: C:\David\\File.txt

The part of the application that deals with file io tries to ensure the file exists prior to doing stuff with a File.Exists(path) call. This call is returning true even for the above example. Can anyone explain why this might be?

The issue I am facing is, beyond one module saving the path incorrectly, certain modules that interact with the file are accepting that incorrect path and working fine while others see it and crash. Although currently I am going to fix this by getting the path saved correctly, I'd like to understand what is going on here.

David Jacobsen
  • 454
  • 3
  • 20
  • 3
    Why do you consider the path `C:\David\File.txt` to be "incorrect"? Second, we cannot say whether `File.Exists` reports the right result or not, since we don't know whether in your case, said file actually exists or not. – stakx - no longer contributing Aug 15 '15 at 15:50
  • I agree, there is nothing "poorly formatted" about that path... – Ron Beyer Aug 15 '15 at 15:56
  • @stakx, we can safely assume that `File.Exists` reports the "right" result. If it would be otherwise, the screams of thousands of developers could be heard from space. (...sticking my head out of the window...listening... no, not hearing anything...) :-b –  Aug 15 '15 at 16:01
  • 1
    @elgonzo: I am not questioning whether `File.Exists` itself works correctly or not, but why the OP considers the observed `File.Exists(@"C:\David\File.txt") == true` to be not correct. Since we don't know whether that file actually exists or not, the question cannot be fully understood. – stakx - no longer contributing Aug 15 '15 at 16:05
  • I will edit the question, stackoverflow ate the error by reading two backslashes as one. The path is being saved as "C:\David\\File.txt" with two backslashes between David and File.txt. – David Jacobsen Aug 17 '15 at 15:22
  • @DavidJacobsen: For future reference, using backticks (\`example\` => `example`) stops escaping and formats the text as code. – 31eee384 Aug 17 '15 at 15:46
  • Maybe following answer will be useful: http://stackoverflow.com/questions/1266674/how-can-one-get-an-absolute-or-normalized-file-path-in-net – kwesolowski Aug 17 '15 at 20:37

1 Answers1

4

You have a false premise: that C:\David\\File.txt is an invalid path. Multiple backslashes are accepted fine in Windows. Try notepad C:\David\\File.txt in a command prompt as an experiment--it should work.

For more info, see this other SO q/a that reaffirms this. Any number of backslashes are fine, and this can be used as a "easy" way to combine paths without worrying about the number of backslashes. For example, the user can provide C:\David or C:\David\ and you can add \test.txt without worrying which input the user provided. However, Path.Combine is the real way to do this in C#.

Edit: To remove your extra \'s easily before passing the path into the other program, try splitting the path into the drive and folder names and combining it back together into a path. Like this:

string path = Path.Combine(pathWithManyBackslashes.Split('\\'));

Because Split doesn't create new entries when the delimiter repeats, you get rid of them. For example, C:\David\\File.txt => [C:, David, File.txt].

Community
  • 1
  • 1
31eee384
  • 2,748
  • 2
  • 18
  • 27
  • Thanks for the information about Path.Combine. I assumed the path was invalid as in the address bar of Windows Explorer, or in the run/search bar through the start menu, C:\david\\ hits an error. C:\david\\file.txt does work though with windows finding the file and using the default app to launch it. Also, technically I think notepad C:\David\\File.txt just means that notepad successfully handles multiple backslashes in it's argument, though the other examples are enough. The error then is that the third party tool we use that handles FTP functions doesn't support multiple backslashes. – David Jacobsen Aug 17 '15 at 17:24
  • 1
    Good point about the command prompt just being more evidence that it's the "way things are" rather than proof. I edited to add a way to normalize the path that might work in your case, in case you hadn't thought of something yet. – 31eee384 Aug 17 '15 at 18:38