user11092881
user11092881

Reputation:

Copy to output directory doesn't work for anything else than content in asp.net

I noticed that when I build using publish, the files I mark as "copy always" for "copy to output directory" and with the "build action" "embedded resources", don't get copied to the output directory when I publish, is there a reason why? It works when it's "build action: content" I am using Visual Studio 2015.

Upvotes: 0

Views: 1026

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

You can see the Microsoft doc Build action values to know the difference between content and Embedded Resource.

A file marked as Content can be retrieved as a stream by calling Application.GetContentStream. For ASP.NET projects, these files are included as part of the site when it's deployed.

The file is passed to the compiler as a resource to be embedded in the assembly. You can call System.Reflection.Assembly.GetManifestResourceStream to read the file from the assembly.

The following code is used for copying the files from the resources to another place.

  protected void Page_Load(object sender, EventArgs e)
    {
        string[] fileNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
        foreach (String fileName in fileNames)
        {
            using (FileStream fileStream = File.Create(@"D:\test\" + fileName))
            {
                Assembly.GetExecutingAssembly().GetManifestResourceStream(fileName).CopyTo(fileStream);
            }
        }
    }

Upvotes: 1

Related Questions