#1
  1. I lov C in AIX/Linux, hate C++
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2003
    Location
    Jacksonville, Florida
    Posts
    1,655
    Rep Power
    34

    C# - Convert FileInfo object??


    Anyone know how to convert this object properly?? (C#)

    Code:
    System.IO.DirectoryInfo di1 = new System.IO.DirectoryInfo("C:\blah");
    System.IO.FileInfo fi1 = di1.GetFiles(sFileName);
    I'm getting an compile error saying "Cannot implicitly convert type 'System.IO.FileInfo[]' to 'System.IO.FileInfo'".

    CType() is not a recognized object in C#. Can't use Convert object either. I tried the "(FileInfo)di1.GetFiles(sFilename)" but no luck either.

    Thanks...
    Last edited by fletchsod; February 18th, 2008 at 10:51 AM.
  2. #2
  3. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    Originally Posted by fletchsod
    Anyone know how to convert this object properly?? (C#)

    Code:
    System.IO.DirectoryInfo di1 = new System.IO.DirectoryInfo("C:\blah");
    System.IO.FileInfo fi1 = di1.GetFiles(sFileName);
    I'm getting an compile error saying "Cannot implicitly convert type 'System.IO.FileInfo[]' to 'System.IO.FileInfo'".

    CType() is not a recognized object in C#. Can't use Convert object either. I tried the "(FileInfo)di1.GetFiles(sFilename)" but no luck either.

    Thanks...
    This returns the first file that matches but will error out if no files match:
    Code:
    System.IO.DirectoryInfo di1 = new System.IO.DirectoryInfo("C:\blah");
    System.IO.FileInfo fi1 = di1.GetFiles(sFileName)[0];
    This gives you access to all the files that match:
    Code:
    System.IO.DirectoryInfo di1 = new System.IO.DirectoryInfo("C:\blah");
    System.IO.FileInfo[] fi1 = di1.GetFiles(sFileName);
    Personally I'd prefer the second way, then if the fi1.Count is 1 or greater continue. To access the first file by calling fi1[0] (etc.)

    -MBirchmeier
  4. #3
  5. Crushing a million faces
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    282
    Rep Power
    94
    [edit] MBirchmeir beat me by moments
    Last edited by Death Goddess; February 18th, 2008 at 11:05 AM.
    Baby soft, because its made from real babies.
  6. #4
  7. I lov C in AIX/Linux, hate C++
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2003
    Location
    Jacksonville, Florida
    Posts
    1,655
    Rep Power
    34
    Ah! That works great! I tried the "[]" after the fi1. ("fi1[]") Must have confused myself with something else.

    Thanks...
    Last edited by fletchsod; February 18th, 2008 at 01:22 PM.
  8. #5
  9. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    [code=C#]di1.GetFiles(sFileName)[0][/code]...is not trustworthy because it could return a 0-length array or null or something. Then the [0] causes an Exception.
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by MBirchmeier
    This returns the first file that matches but will error out if no files match:
    Code:
    System.IO.DirectoryInfo di1 = new System.IO.DirectoryInfo("C:\blah");
    System.IO.FileInfo fi1 = di1.GetFiles(sFileName)[0];
    This gives you access to all the files that match:
    Code:
    System.IO.DirectoryInfo di1 = new System.IO.DirectoryInfo("C:\blah");
    System.IO.FileInfo[] fi1 = di1.GetFiles(sFileName);
    Personally I'd prefer the second way, then if the fi1.Count is 1 or greater continue. To access the first file by calling fi1[0] (etc.)

    -MBirchmeier
    Old post, but I found it while googling some of my own issues-- thought I'd add some clarity.

    The second way isn't just the preferred way, but it is the right way. DirectoryInfo.GetFiles(string filename) returns a type FileInfo[] no matter what you pass in as the parameter. So you can't set it to FileInfo fi1 because FileInfo and a FileInfo[] array object are not the same thing. The reason fi1 = di1.GetFiles(filename)[0] works is because you reference the first FileInfo object in the returned array. But as stated previously- there's no built in error trapping if no file is found.

    Even though you told the di1 object to only look for one file, it is still going to return it in an array. Best practice would be
    Code:
    FileInfo[] fi1 = di1.GetFiles(filename);
    //iterate through the fi1 array to verify only one file was found
    if(fi1.Length>1)
    //more than one file matched the searchPattern
    else if(fi1.Length ==0)
    //nothing matched the searchPattern
    else
    //do whatever you want to do with the one file you found using fi1[0]
    //or creating a FileInfo f = fi1[0];
    Old post, but there's my two cents.

IMN logo majestic logo threadwatch logo seochat tools logo