pthivierge

How to use PIConfig with .Net (C#)

Discussion created by pthivierge on Dec 5, 2013
Latest reply on Dec 16, 2013 by mhamel

Did you ever wonder how to control PIConfig from a .Net application ?  

 

There is a way to do this by using the Process Class.  I wrote a really basic exemple here that executes the pisysdump.dif.  Like I said, it is so simple that it has to be run in the PI dat folder and all path are hard coded.  (so for me it is really bad code but I wanted to share it anyway).  Let me know if this helps and also ideas you have to use it... we could shurely improve it.

 

 To use it simply use the command line and call "program.exe" in the pi\dat folder.

 

 

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

namespace PIConfigSimpleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
           // configures the file to pass to PIConfig
            var fileInfo = new FileInfo("pisysdump.dif");
            Process process = new Process();
            
            // Configure the process using the StartInfo properties.
            process.StartInfo.FileName = @"piconfig.exe";
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();           

            using (var streamReader = new StreamReader(fileInfo.FullName))
            {
                process.StandardInput.Write(streamReader.ReadToEnd());
                process.StandardInput.Flush();
            }
            process.StandardInput.Close();
            Console.Write(process.StandardOutput.ReadToEnd());

            process.WaitForExit();// Waits here for the PIConfig process to exit.

        }
    }
}

 

 

 Sources: 

 

http://stackoverflow.com/questions/181719/how-to-start-a-process-from-c

 

http://stackoverflow.com/questions/2956274/how-to-pass-filename-to-standardinput-process-in-c

Outcomes