Store files in C# EXE file

前端 未结 4 1704
后悔当初
后悔当初 2021-01-07 03:25

It is actually useful for me to store some files in EXE to copy to selected location. I\'m generating HTML and JS files and need to copy some CSS, JS and GIFs.

Snipp

4条回答
  •  情书的邮戳
    2021-01-07 03:56

    Add files to project resources and set their "Build Action" as "Embedded Resource".

    Now extract any file (text or binary) using this snippet:

    WriteResourceToFile("Project_Namespace.Resources.filename_as_in_resources.extension", "extractedfile.txt");
    
    
    public static void WriteResourceToFile(string resourceName, string fileName)
    {
        int bufferSize = 4096; // set 4KB buffer
        byte[] buffer = new byte[bufferSize];
        using (Stream input = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
        using (Stream output = new FileStream(fileName, FileMode.Create))
        {
            int byteCount = input.Read(buffer, 0, bufferSize);
            while (byteCount > 0)
            {
                output.Write(buffer, 0, byteCount);
                byteCount = input.Read(buffer, 0, bufferSize);
            }
        }
    }
    

    Don't know how deep is it correct according to this article: http://www.yoda.arachsys.com/csharp/readbinary.html but it works.

提交回复
热议问题