Search code examples
c#wcf

how to invoke cmd command from WCF library C#


I have a C# WCF library and I want to invoke the cmd command from one of the method in WCF library, but when I run the code and call that method, it neither executes cmd command nor generates any kind of exception, what should I do, my code is as under..please someone guide me.

I have already verified the command on cmd, it executes successfully from the cmd , but not from the WCF library, so there is no any problem in command syntax.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Info.FileName = "cmd.exe";
Info.Arguments= 
         "\"" + tallyPath + "\"" + "  " 
         + "/TDL:" + tdlPath + " " + "/LOAD:" 
         + cmpCode + "  " + "/SETVAR:SVVarUN:" 
         + uname + " " + "/SETVAR:SVVarPass:"
         + pwd;

proc.StartInfo = startInfo
proc.Start();

Solution

  • Edited answer - I have built the simplest of WCF services to try and mimic your scenario as best as possible. I have tested this and it works fine (pasted below). Pay attention to the commented out lines that didn't seem to look right in your example above. You also had an issue with your StartInfo - you create is as "Info" but set "proc.StartInfo = startInfo" which does not seem to exist - it should be set to just "Info".

    using System.Diagnostics;
    using System.ServiceModel;
    
    namespace WcfService1
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string RunTally();
        }
    
        public class Service1 : IService1
        {
            public string RunTally()
            {
                var tallyPath = "C:\\temp\\";
                var tallyExe = "tally.exe";
                var cmpCode = "myCmpCode";
                var uname = "myUname";
                var pwd = "myPwd";
                var tdlPath = "myTdlPath";
    
                Process proc = new Process();
                ProcessStartInfo info = new ProcessStartInfo();
                info.WindowStyle = ProcessWindowStyle.Hidden;
                info.RedirectStandardOutput = true;
                info.RedirectStandardInput = true;
                info.RedirectStandardError = true;
                info.UseShellExecute = false;
                info.Arguments =
                    // "\"" + tallyPath + "\"" + "  "
                     // +
                     "/TDL:" + tdlPath + " " + "/LOAD:"
                     + cmpCode + "  " + "/SETVAR:SVVarUN:"
                     + uname + " " + "/SETVAR:SVVarPass:"
                     + pwd;
                info.FileName = tallyPath + tallyExe;
    
                proc.StartInfo = info;            
                proc.Start();
    
                var textReceived = "";
                while (!proc.StandardOutput.EndOfStream)
                {
                    textReceived += proc.StandardOutput.ReadLine();
                }
    
                return string.Format("The call returned: " + textReceived);
            }
        }
    }