-1

I am new to C# and I have normally built windows forms using VB and was able to use one code to open any embedded file I added to my "Resources". As far as C# I have looked online for hours and have yet to find anything that worked. Please assist in any way that you can.

I have a Windows Form that will have a single button that will be assigned to open a particular file I have added to the "Resources" folder. Usually I would use the following code to have a Button_Click to load an exe, doc or pdfile. I am looking for something similar for C#.

VB Code:

IO.File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Temp & "\IEResetConfigure.exe", My.Resources.IEResetConfigure)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\IEResetConfigure.exe")
rishit_s
  • 350
  • 3
  • 12
Nathan
  • 3
  • 5

3 Answers3

1

Simply write your resource file to temporary directory and run the file

using System;
using System.IO;
using System.Diagnostics;

// ...
    byte[] resourceFile = Properties.Resources.Newspaper_PC_13_12_2013;

    string destination = Path.Combine(Path.GetTempPath(), "Newspaper_PC_13_12_2013.pdf");
    System.IO.File.WriteAllBytes(destination, resourceFile);
    Process.Start(destination);
Bilal
  • 114
  • 11
  • Thanks man that worked like a charm, I thought I had it where I could use on different machines but I did not and your code worked flawlessly. Much appreciated! – Nathan Feb 23 '17 at 15:50
  • One other question. Now that this is sending a file to the %temp% directory can that be auto deleted after closing out of the doc? Or will changing copy to output directory "do not copy" fix that? – Nathan Feb 23 '17 at 15:52
  • You can try to delete file every 2 seconds. There might be a better solution to this bad hack. I'm just a student. – Bilal Feb 23 '17 at 22:10
0

Example of my comment

using System;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;

// ...
    static void Main(string[] args)
    {
        byte[] resourceFile = Properties.Resources.Newspaper_PC_13_12_2013;

        string destination = Path.Combine(Path.GetTempPath(), "Newspaper_PC_13_12_2013.pdf");
        File.WriteAllBytes(destination, resourceFile);
        Process.Start(destination);

        AutoDelete(2000, destination);

        Console.Write("Press any key to quit . . . ");
        Console.ReadKey(true);
    }

    static async void AutoDelete(int milliseconds, string destination)
    {
        while (File.Exists(destination))
        {
            await Task.Delay(milliseconds);

            try
            {
                File.Delete(destination);
            }

            catch
            {
                continue;
            }
        }
    }
Bilal
  • 114
  • 11
  • Thank you Mooncat you are assisting me greatly. Student or not you are helping very much to steer me in the right direction. – Nathan Feb 23 '17 at 22:57
0

For anyone still looking, Here is a way of opening an "embedded" file. I'd love for someone to correct me below on a better way.

The first part is to make sure your file is added to your project in the bin\debug folder. I then used this code to call it

    private void button1_Click(object sender, EventArgs e)
    {
        //Place file in .\bin\Debug folder of project
        string filename = "YourFileName.pdf";
        System.Diagnostics.Process.Start(filename);

For full Disclosure this whole part has been stolen from (Opening a .pdf file in windows form through a button click)

I did however run into an issue where after the first build that didn't work for me. So when I created the setup project I added the "Project Output" and then added in my pdf via "add file" to the application folder.

That has continued to work flawlessly for me since. This is my first post on Stack Overflow, so please let me know if I misunderstood any rules or could improve. Thank you and I hope this helped!

GBann
  • 1
  • 1
  • This does save the file in a manner that the user can access it outside of the software, and therefore they can delete it. It answers your question, but I would consider it bad practice to do so. However it is a quick & simple solution if its not for a production environment. – GBann Feb 13 '23 at 10:13