Monday, December 14, 2009

Locating a file placed in the bin directory when Build Action is set to Content

Task: How could a get a content file from the bin directory without having to hard code the directories that are created under the bin when a project is built. Using reflection I get the Executing Assembly’s Codebase (Note: You could also use Assembly.GetExecutingAssembly().Location then there’s no need for the replace in line 3)

Code Snippet
  1. Dim pathToMyFile As String = String.Empty
  2. With Assembly.GetExecutingAssembly().CodeBase
  3.     Dim tmpPath As String = .Replace("file:///", String.Empty)
  4.     pathToMyFile = tmpPath.Substring(0, tmpPath.LastIndexOf("bin") + 4)
  5.     Dim directoryList() As String = Directory.GetFiles(pathToMyFile, "MyFileName.docx", SearchOption.AllDirectories)
  6.     For Each myFile In directoryList
  7.         pathToMyFile = myFile
  8.     Next
  9. End With

No comments:

Post a Comment

Calling .NET From COM Originally posted by Mikec276 on C Sharp Friends It might be hard to convince your IT manager to let you build y...