-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
  • 11
Nathan
  • 3
  • 5

2 Answers2

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