How to open a file such as pdf, etc. "from a Windows Form application in C #

I want to create a window form containing a link, for example, when a user clicks on this link, a file with a certain format (for example, a pdf file or html file) is opened, which is added to the resources. it will not be opened in the form, this means that the file will be opened from the program using Adobe Reader or another program. How can i do this? Thanks you

+4
source share
2 answers

You will have to extract this file from the resources (I assume that we are talking about building the embedded resources here) up to %temp% and then just Process.Start() . Make sure the extracted file has the appropriate extension.

+5
source

You can do this using Process.Start :

 System.Diagnostics.Process.Start(@"C:\myfolder\document.pdf"); 

If the file is an embedded resource, you first need to extract it and save it to disk. You cannot open a document from the stream directly, because third-party applications will not be able to access the memory of your process:

 string resourceName = "test.pdf"; string filename = Path.Combine(Path.GetTempPath(), resourceName); Assembly asm = typeof(Program).Assembly; using (Stream stream = asm.GetManifestResourceStream( asm.GetName().Name + "." + resourceName)) { using (Stream output = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = new byte[32 * 1024]; int read; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } } } Process.Start(filename); 
+3
source

All Articles