Search code examples
c#.net-assemblydirectory-structure

Getting folder content in same project or dll thorough Reflection


#Get current Assembly information through Reflection.

protected static string GetCurrentFolder()
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);
}  

Solution

    • Access folder after get the current assembly information.
    • Add folder in the corresponding path first.
     var file = File.ReadAllText(Path.Combine(GetCurrentFolder(), @"Documents\Template.html"));
    
    

    #Without Reflection you can use the below command,but make sure you copy the file in bin -> debug folder Set the property of file Copy to Output Directory as "Copy Always" from visual studio.(Create a folder for your document as paste the file in visual studio)

    File.OpenRead(Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory, @"Documents\Template.html"));