Start process in the background with C #?

0

I've been trying for a while, but I can not execute excel in the background.

What I want is to do a service that opens an excel book every 30 seconds and closes it later.

    static void Main(string[] args)
    {
        while (true)
        {



            String ruta = Directory.GetCurrentDirectory().ToString();
            Process p = new Process();


            //Propiedades del proceso
            p.StartInfo = new ProcessStartInfo("Exelcito.xlsm");
            p.StartInfo.WorkingDirectory = ruta;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;


            //Iniciamos
            p.Start();
            Thread.Sleep(1000 * 5);//Carga del excel + ejecucion de macros
            //Matamos
            p.Kill();
            Thread.Sleep(1000 * 25);

        }


    }

It happens that I need to run in the background and appear in the foreground, this has to run on the computer of one of the managers and it has to be in the background so as not to interrupt whatever it is doing.

    
asked by Aritzbn 13.11.2017 в 15:25
source

2 answers

2

I would recommend that you evaluate using api de office or some based on open xml to access excel.

I would recommend you evaluate the library ClosedXml

Loading and Modifying Files

You'll see in the example that you can open an existing excel

var workbook = new XLWorkbook("BasicTable.xlsx");

apply changes and burn them.

The good thing is that by using this library you can access excel without having a local office installed on the PC.

The library references using nuget

ClosedXML nuget

    
answered by 13.11.2017 / 20:04
source
-2

For this, create a Windows service:

File> New> Project

In this window, on the left side select:

Templates> Visual C #> Windows> Classic Desktop> Windows Service.

For more information you can see the following video: link

    
answered by 14.11.2017 в 23:53