1

I have an exe file which I run through windows command prompt and give command line arguments. I went through this post and ran the following command:

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

But all it did, is to give me resource files located in WindowsFormsApplication1\obj\Debug folder

I went through this post but it tells on how to execute the exe directly without the running it from cmd.

I even tried the following command:

string path = Path.Combine(Path.GetTempPath(), "MyApplication.exe");

It worked but after clearing my C:\Users\UserName\AppData\Local\Temp folder the application started giving an error.

I even tried the following command:

global::ApplicationName.Properties.Resources.MyApplication

but it gives byte[] and not the path to the application.

All I want to know is how to run the application which is embedded in my resources so that I can successfully execute the following command:

var proc = new Process
                    {
                        StartInfo = new ProcessStartInfo
                        {
                            FileName = "cmd.exe",
                            Arguments = "/K " + MyApplication+" Argument "+Path1+" "+Path2 ,
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            CreateNoWindow = true
                        }
                    };
                    proc.Start();

                    while (!proc.StandardOutput.EndOfStream)
                    {

                        string line = proc.StandardOutput.ReadToEnd();
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(resultFile))
                        {
                            file.WriteLine(line);
                        }
                    }
Community
  • 1
  • 1
Priyanka
  • 107
  • 13
  • You have an embedded file as a resource in your project? If so you must extract it to a directory in the filsystem before you run it. – Dimitris Fousteris Oct 20 '14 at 23:41
  • Yes I have added the application file as a resource. How do I extract it and run it then ? Can i delete it after running it so that the application does not create any temporary junk files ? – Priyanka Oct 20 '14 at 23:44

1 Answers1

1

Extract the resource into a file in the filesystem and then run it.

byte[] fileContents = ApplicationName.Properties.Resources.MyApplication;
File.WriteAllBytes("MyApplication.exe", fileContents);

Now you can run the file using MyApplicaton.exe as path.

Dimitris Fousteris
  • 1,091
  • 8
  • 12
  • I tried the following command: byte[] fileContents =global::ApplicationName.Properties.Resources.MyApplication; File.WriteAllBytes("MyApplication.exe", fileContents); MessageBox.Show(MyApplication.exe); but it gives me error – Priyanka Oct 20 '14 at 23:52
  • Why are you doing messagebox cloc.exe? The code fragment i have written creates a file in the current directory so you can run it using process.start – Dimitris Fousteris Oct 20 '14 at 23:55
  • I don't want to run the application as it is. i want to run it through command line so i can give it arguments. I did MessageBox.Show to see if it's giving me the path – Priyanka Oct 20 '14 at 23:57
  • Ok so use a full path of your choice to extract your executable and then pass the same path to the cmd – Dimitris Fousteris Oct 21 '14 at 00:06
  • I am a beginner to C# could you please explain me how to do that – Priyanka Oct 21 '14 at 00:08
  • Just run the what @Priyanka suggested, and see if it created a file in the current directory (current of the running `.exe`, perhaps `\bin\Debug\` of your project). If it does, run your code, but you don't need to run `cmd.exe` you can run the `"MyApplication.exe"` and pass it the arguments (you already have most of the code written in your question... – nurchi Oct 21 '14 at 00:43