locked
ArgumentException : Illegal characters in path RRS feed

  • Question

  • I have ArgumentException problem which said Illegall characters in path.

    static IEnumerable<FileInfo> GetFiles(params string[] filepaths)
      {
       foreach (var item in filepaths)
       {
        yield return new FileInfo(item);
      }
      }
    

    Initially it work well if IEnumerable<FileInfo> declare in this manner

    IEnumerable<FileInfo> Folder1 = GetFiles( @"C:\temp\folder1\text1.txt", @"C:\temp\folder1\text2.txt");
    IEnumerable<FileInfo> Folder2 = GetFiles( @"C:\temp\folder2\text1.txt", @"C:\temp\folder2\text2.txt");
    

    But when i tried to change it into this, the exception occur.

    IEnumerable<FileInfo> Folder1 = GetFiles(@"C:\temp\folder1\*.*");
    IEnumerable<FileInfo> Folder2 = GetFiles(@"C:\temp\folder2\*.*");
    

    where did I do wrong? I'm trying to make it to read all the files in the folder without typing in the files' name in the GetFiles() method.


    kok loong
    Wednesday, June 1, 2011 1:08 AM

Answers

  • Use below code instead of your GetFiles method:

    foreach (string fileName in Directory.GetFiles("C:\temp\folder1", "*.*"))
    {
      yield return new FileInfo(fileName);
    }
    


    Make everything as simple as possible, but not simpler.
    • Marked as answer by kokloong Friday, June 3, 2011 1:42 AM
    Wednesday, June 1, 2011 1:32 AM

All replies

  • Use below code instead of your GetFiles method:

    foreach (string fileName in Directory.GetFiles("C:\temp\folder1", "*.*"))
    {
      yield return new FileInfo(fileName);
    }
    


    Make everything as simple as possible, but not simpler.
    • Marked as answer by kokloong Friday, June 3, 2011 1:42 AM
    Wednesday, June 1, 2011 1:32 AM
  • Hi Kokloong;

    The Illegal characters in the path is *. The FileInfo constructor takes a file name , "You can specify either the fully qualified or the relative file name", NO wild card characters.

     

     

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, June 1, 2011 3:47 AM
  • Any update? Would you mind letting us know how it goes?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 2, 2011 10:57 AM
  • Hi all, I resolve this error after refer to Cem Bilal Özkan's given idea. thank alots to all for helping and guiding me.
    kok loong
    Friday, June 3, 2011 1:43 AM