How to open a PDF file that is also a project resource?

后端 未结 8 1969
星月不相逢
星月不相逢 2021-01-17 19:35

I have a PDF file that I have imported in as a resource into my project. The file is a help document so I want to be able to include it with every deployment. I want to be a

8条回答
  •  遇见更好的自我
    2021-01-17 19:50

    This should help - I use this code frequently to open various executable, documents, etc... which I have embedded as a resource.

    private void button1_Click(object sender, EventArgs e)
       {
        string openPDFfile =  @"c:\temp\pdfName.pdf";
        ExtractResource("WindowsFormsApplication1.pdfName.pdf", openPDFfile);
        Process.Start(openPDFfile);
       }
    
         void ExtractResource( string resource, string path )
                {
                    Stream stream = GetType().Assembly.GetManifestResourceStream( resource );
                    byte[] bytes = new byte[(int)stream.Length];
                    stream.Read( bytes, 0, bytes.Length );
                    File.WriteAllBytes( path, bytes );
                }
    

    enter image description here

提交回复
热议问题