0

I have written a simple program in C# that saves to a text file every 5 seconds. I have also made a program in electron using node.js, is there a way I can start the C# program through electron?

I have tried compiling the C# program as an exe and running it that way but I couldn't get it to work.

Any help will be greatly appreciated!

Answer The problem was that my C# file needed to be run as an administrator, I used the function below;

var exec = require('child_process').execFile;

var fun =function(){
   console.log("fun() start");
   exec('HelloJithin.exe', function(err, data) {  
        console.log(err)
        console.log(data.toString());                       
    });  
}
fun();
Dan
  • 83
  • 1
  • 9
  • 1
    Could you specify: "I couldn't get it to work." Did you get any errors? – TheTanic Feb 11 '21 at 10:53
  • You can use [IPC](https://www.electronjs.org/docs/api/ipc-main) to send a request from renderer to the main thread and start the exe process. Check this Q/A [Execute an exe file using node.js](https://stackoverflow.com/questions/19762350/execute-an-exe-file-using-node-js). – Christos Lytras Feb 11 '21 at 10:55
  • Thank you for your help, I used the function that you linked to and had to run my node js as an admin. Thanks! – Dan Feb 11 '21 at 11:25

1 Answers1

1

You could just run a command with nodejs that starts the c# program.

const { exec } = require("child_process");

exec("cmd /K 'C:\SomeFolder\MyApp.exe'", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

DISCLAIMER: i haven't tested it because i am not on windows right now

source:

chip coint
  • 51
  • 6
  • While I don't get any errors using this it appears the program isn't actually running, is there an easy way to force the file to run as admin in node js. Thanks – Dan Feb 11 '21 at 11:24
  • Possible that ```cmd /K 'C:\SomeFolder\MyApp.exe'``` just doesn't work. Option 1: Change it so you don't need admin permissions. Option 2: If you need admin permissions i would suggest putting the admin stuff in the c# program, because running a command as admin in nodejs is not that easy on windows. Else i think this [site](https://nicedoc.io/coreybutler/node-windows) would be the best package to use. That wouldn't just start your program, it creates a service with it, which is not what you want i think. – chip coint Feb 11 '21 at 11:55