Blog Archives

directory info get files show directories C# .Net

There is sometimes a misunderstanding between a “file” and a “folder” in filesystems. In C# .Net the following is often used to list all “files” in a folder.

DirectoryInfo di = new DirectoryInfo("yourpath");

foreach (FileInfo fi in di.GetFiles()) {
//do work
}

However, even though you can specify to search containing subdirectories, the above function will not inherently list folders. If you are looking for the equivalent to “dir” or “ls”, instead use “GetFileSystemInfos()”.

            DirectoryInfo di = new DirectoryInfo("yourpath");
		
//note the difference here with getfilesysteminfos
		foreach (dynamic d in di.GetFileSystemInfos()) {
}

Note the usage of dynamic in the above example compared to the first example. This avoids any potential issues with inheritance and choosing the right class for your temp iterator variable for unboxing etc.

Advertisement

Change ReadOnly File Attribute in C#

Keep in mind when deleting files from code the typical override allowing users to delete anything in their path, whether it is marked readonly or not, will prevent your IO process from continuing and throw a nice little generic “access denied” error.

To resolve this, simply remove the readonly attribute on the file prior to deletion.

     if (File.GetAttributes(fullfilename) == FileAttributes.ReadOnly)
                    {
                        File.SetAttributes(fullfilename, FileAttributes.Normal);
                    }

                    File.Delete(fullfilename);

Alternatively, using FileSystemInfo (unmodified courtesy of Ken White):

private static void DeleteFileSystemInfo(FileSystemInfo fsi)
{
    fsi.Attributes = FileAttributes.Normal;
    var di = fsi as DirectoryInfo;

    if (di != null)
    {
        foreach (var dirInfo in di.GetFileSystemInfos())
            DeleteFileSystemInfo(dirInfo);
    }

    fsi.Delete();
}

References
Wordpress (Imran Akram), http://imak47.wordpress.com/2009/01/15/how-to-remove-readonly-attribute-from-a-file/
StackOverflow (Ken White), http://stackoverflow.com/questions/667381/programatically-run-cmd-exe-as-adminstrator-in-vista-c