0

My WF application requires of an excel file to work properly. Is there any way to add that file to the project and, when the user install my application, it automatically places itself in a default path?

Thanks

1
  • 1
    Is the Excel file going to be changed by the application at runtime (thus needing to be installed in a directory with write permissions for a non-admin user), or is it just a resource that could be loaded from wherever you decide to put it? If the latter, I recommend including it as an embedded resource and reading it directly from the assembly at runtime. Commented Jan 9, 2015 at 15:23

2 Answers 2

1

You can embed the file in your executable assembly:

  1. Add the file to the project.

  2. Select the file in the Solution Explorer and open Properties (F4).

  3. Set the "Build Action" to "Embedded Resource".

  4. In your code, load the resource stream and copy it to the target file stream:

    var assembly = Assembly.GetExecutingAssembly();
    using (var resourceStream = assembly.GetManifestResourceStream("YourNamespace.ExcelFile.xlsx"))
    using (var targetStream = File.OpenWrite("targetFile.xlsx"))
    {
        resourceStream.CopyTo(targetStream);
    }      
    

    Make sure that YourNamespace matches the namespace of the directory structure where you have put the Excel file in your project.

This Knowledge Base article has a detailed explanation of the procedure.

Sign up to request clarification or add additional context in comments.

2 Comments

System.Reflection.Assembly
Awesome! Works perfect!
0

Add the file to your project and set its build action to "Copy if newer." After that, it ought to be deployed along with your binaries when the application is installed. However, note that this copy should be considered as read-only.

If you need to save changes to the file after installation then you copy it into the application data folder within the user's profile:

var appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var writableFilePath = Path.Combine(appDataFolder, "MyApplication\\MyExcelFile.xlsx");
// ensure directory exists before copying to it:
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(writableFilePath));

... and use this file instead of the read-only version whenever it already exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.