-1

I'm trying to read a file to get the path and then search that path.

C:\Users\Public\Documents\ScriptPath.txt contains "C:\Users\Public\Music Folder\"

and here is my code

        string ScriptPath = System.IO.File.ReadAllText(@"C:\Users\Public\Documents\ScriptPath.txt"); 

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(ScriptPath);
        System.Threading.Thread.Sleep(2000);

        foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
        {
            MessageBox.Show("{0}, {1}" + file.Name+ file.Length);
        }

The Error I get is this.

ArgumentException was unhandled

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Illegal characters in path.

If anyone could help with understanding why this doesn't work I would be grateful.

  • "C:\Users\Public\Music Folder\" It seems it's the spaces in the path directory that gives it Illegal characters in the path... anyone got a fix? – user3128006 Apr 29 '17 at 11:11
  • what line is throwing exception? Make MessageBox.Show(ScriptPath); and be sure about that var content. – Fran Cerezo Apr 29 '17 at 11:38
  • foreach (System.IO.FileInfo file in dir.GetFiles("*.*")) is throwing the error. It's because of the spaces in the scriptpath are illegal characters eg spaces in folder names. not sure how to fix it though – user3128006 Apr 29 '17 at 11:47

1 Answers1

0

Try

foreach (System.IO.FileInfo file in dir.GetFiles("*"))
Fran Cerezo
  • 940
  • 3
  • 8
  • 19
  • That seems to work fine with "C:\Users\Public\Music Folder\" but only when my text file it all lowercase. Thank you. – user3128006 Apr 29 '17 at 12:03
  • Windows is not case sensitive, but you can use ScriptPath.ToLower() If was useful please mark as valid. I took the info from https://msdn.microsoft.com/es-es/library/8he88b63(v=vs.110).aspx – Fran Cerezo Apr 29 '17 at 13:01