Search code examples
c#visual-studioprojects-and-solutions

Taking a copy of an Excel template that is saved in the Solution


I have this solution structure:

enter image description here

I'd like to take a copy of this template and then save a copy of it to a network drive. Using System.IO I've previously using this code to take a copy of a file:

string templateFilePath = @"\\blah\blah\blah\blah\Temp.xlsx";   //<<<< X
string exportFilePath = @"\\blah\blah\blah\blah\Results.xlsx";
File.Copy(templateFilePath, exportFilePath, true);

Because the template is saved within the solution do I still need to specify the complete pathway or is there a shorter was of referencing this file?


Solution

  • You'll need to specify the full path of the file or the relative path in regards to where the executable is running. So you can set targetFileName = ".\template.xlsx

    another way to get the file is to mark the Build Action in the properties of the file and set it to Embedded Resource. then use the following code to get the stream. Not sure if the stream will help you or not.

    Assembly asm = Assembly.GetExecutingAssembly();
    string file = string.Format("{0}.Template.xlsx", asm.GetName().Name);
    var ms = new MemoryStream();
    Stream fileStream = asm.GetManifestResourceStream(file);