Search code examples
c#c#-4.0choetl

how to handle spaces in file names in path


I want to access a file like :

new ChoJSONReader(@"0_Target Finds.json")

but I tried all possible ways to access like:

new ChoJSONReader("'0_Target Finds.json'")

Nothing worked for me.. Is anyone knows please help me in this


Solution

  • The problem is that the ChoJSONReader handles relative filenames in a special way. I suppose it uses the folder that the assembly is located at instead of using the current working directory. You can fix your error by supplying a full path to the constructor.

    If you are sure that the file is located in the current directory, you can get the full path like this:

    var fullPath = System.IO.Path.GetFullPath("0_Target Finds.json");
    new ChoJSONReader(fullPath)
    

    If you know the directory that the file is located at, it is better to use a specific path and not rely on the current working directory. You can use the Environment.GetFolderPath and the methods of the System.IO.Path class to assemble the path.