I Love the File/Directory Classes in .NET!

I just had a situation where a potential client gave me a zip file of about 200 files. They wanted us to give them an estimate on how long it would take us to recreate each screen in ASP.NET 2.0. I did not relish the thought of typing in all those file names into an Excel spreadsheet so I could assign hours to each one. So I wrote a simple little utility to grab each file in the folder, and add that file name to a StringBuilder object. I separated each file name with a CRLF. Once I had all of the files in the StringBuilder object, I simply copied the text to the Clipboard. Then I opened up Excel and pasted the contents of the Clipboard into an Excel column. Bingo! I now had all those file names into Excel, ready for me to assign hours to!

C# Code
private void GetFiles()
{
  System.Text.StringBuilder sb = new System.Text.StringBuilder(1024);
  System.IO.FileInfo fil;

  foreach (string strFile in System.IO.Directory.GetFiles(@"D:\Customers\ASP"))
  {
    fil = new System.IO.FileInfo(strFile);
    sb.Append(fil.Name.Replace(".asp", "") + Environment.NewLine);
  }

  Clipboard.SetText(sb.ToString());
}

VB.NET Code
Private Sub GetFiles()
  Dim sb As New System.Text.StringBuilder(1024)
  Dim fil As System.IO.FileInfo

  For Each strFile As String In System.IO.Directory.GetFiles("D:\Customer\ASP")
    fil = New System.IO.FileInfo(strFile)
    sb.Append(fil.Name.Replace(".asp", "") & Environment.NewLine)
  Next

  Clipboard.SetText(sb.ToString())
End Sub

I hope this little tip helps you the next time you have to do something like this!

Paul

Past Blog Content

Blog Archive

4 Comments

Comments have been disabled for this content.