4

I have a database table that containing file paths of excel files that I import using a C# script.

The script works fine unless the filepath contains spaces e.g. C:\Temp\My Excel File.xls and I get an Illegal characters in path error message. Unfortunately I am not able to change the file names at the source.

If I hard code the file path to be as below it works fine.

String Filepath = @"C:\Temp\My Excel File.xls";

How do I alter this so I can include a string variable that will store the filepath from the database e.g.

String Filepath = //Code to get FilePath from database

StringCorrectedFilePath = @+FilePath;

Thanks in advance of any help

Edit: Issue is caused by files that start with a number creating invalid escape sequence. e.g. C:\Temp\20160611 My Excel File.xls

Edit 2: SOLVED - Error was caused by carriage return characters appearing after the file extension. Please see my answer for the solution.

Simon
  • 1,293
  • 5
  • 21
  • 39
  • If it works with spaces when hard-coding the variable that means that the file path you're getting from the database probably doesn't actually contain spaces. What is the actual value you're getting from the database? – Kyle Jun 10 '16 at 20:21
  • There are definitely spaces in the file path, I am looking at the database table now. – Simon Jun 11 '16 at 06:28

6 Answers6

6

Whether you do this

String Filepath = @"C:\Temp\My Excel File.xls";

or this

String Filepath = "C:\\Temp\\My Excel File.xls";

the string stored in memory is just C:\Temp\My Excel File.xls, whatever the debugger may tell you. So when you read some string from somewhere (database, file, user input, ...) you don't need to "escape" backslashes. So just use that string.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
  • Thanks for your answer, But its not the backslashes causing the issue, its when the file path contains spaces e.g. This file will work "C:\Temp\MyExcelFile.xls" but this one will not "C:\Temp\My Excel File.xls" – Simon Jun 11 '16 at 06:34
  • Apologies, ignore my previous comment. I've got more information. The problem only occurs when the filename starts with a number i.e. "c:\temp\20160611 My Excel File.xls" – Simon Jun 11 '16 at 07:11
  • @SimonTindall How are you using that path? You should add that to your question. – Hans Kesting Jun 11 '16 at 09:45
  • Thanks for all your help guys, turns out the illegal characters were carriage returns appearing after the file extension. I wouldnt have found the solution if it wasnt for your pointers and i've upvoted accordingly! – Simon Jun 12 '16 at 06:23
2

Path.GetInvalidFileNameChars

FilePath = string.Concat(FilePath.Split(System.IO.Path.GetInvalidFileNameChars())).Trim();
Slai
  • 22,144
  • 5
  • 45
  • 53
1

Well you can replace blank space with %20 character and while retrieving replace back with blank space again like (you may as well choose to use regular expression for the same)

String Filepath = @"C:\Temp\My Excel File.xls";
Filepath = Filepath.Replace(" ", "%20");

While retrieving back

string mypath = pathyouhavegotfromDB.Replace("%20", " ");
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

I think you need to put quotation marks around the path with spaces.

string filepath = @"C:\Temp\My Excel File.xls";            
filepath = $"\"{filepath}\"";
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
0

Thanks for everyone's help, I tried all of these and unfortunately they didn't work which led me to believe that the issue wasn't what I originally thought.

It turns out that the files causing the Illegal characters in path all had carriage return characters at the end of the file name, after the file extension.

To resolve this I used the following code and now it works perfectly

FilePath = FilePath.TrimEnd('\r', '\n');

Thanks everyone for your help.

Simon
  • 1,293
  • 5
  • 21
  • 39
-1

Try this:

String StringCorrectedFilePath = @""+ Filepath;
taquion
  • 2,667
  • 2
  • 18
  • 29