none
How to call cs file(C#) from a batch script(.bat)???? RRS feed

  • Question

  • I have a batch file
    @ECHO off
    CD  C:\MF
    rem SETTING THE LOCATION OF THE C# COMPILER
    SET framework=%SystemRoot%\Microsoft.NET\Framework\v1.1.4322
    rem COMPILING THE CS FILE
    %framework%\csc  GenerationOfFiles1.cs
    rem EXECUTING  THE C# APPLICATION
    echo GenerationOfFiles1 GeneratePDFReports(%1,%2,%3,%4,%5,%6)


    I am Trying to call cs file(c#) GenerationOFfiles1.cs from the above batch script which has the following method
    public class GenerationOfFiles1
    {
    public void GeneratePDFReports(string reportClassName, string FileDirectory, string FileName, string SQLStatement, string FileDir,string Library)
    }
    I am unable to figure out how to pass .bat file parameters to the cs file and how to call cs file from the bat file????

    Wednesday, December 23, 2009 7:13 PM

Answers

  • Re Yasser's advice: Read documentation about parsing arguments to C# applications from command line - http://msdn.microsoft.com/en-us/library/cb20e19t.aspx.
    Re: How to use C# script: Search the internet - for example: http://www.codeproject.com/KB/cs/cs-script_for_cp.aspx

    If you cannot make GeneratePDFReports your Main method, then let your Main method take class/method name as parameter and use Reflection to run it from your Main method. That is however quite advanced coding.

    -Karel

    Thursday, December 24, 2009 2:40 AM
    Moderator
  • Thanks Yasser ..But in this scenario i am unable to figure it out how to do that....Do you have any examples in this scenario...I didnot find anything in msdn or google 

    rem GenerationOfFiles1.bat file
    @ECHO off
    CD  C:\MF
    rem SETTING THE LOCATION OF THE C# COMPILER
    SET framework=%SystemRoot%\Microsoft.NET\Framework\v1.1.4322
    rem COMPILING THE CS FILE
    %framework%\csc  GenerationOfFiles1.cs
    rem EXECUTING  THE C# APPLICATION
    GenerationOfFiles1 "A" "B" "C" "D" "E" "F"

    ========================================================
    //GenerationOfFiles1.cs file
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GenerationOfFiles1
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length != 6)
                {
                    Console.Write("Usage: GenerationOfFiles1 string reportClassName, string FileDirectory, string FileName, string SQLStatement, string FileDir, string Library");
                    return;
                }
                GeneratePDFReports(args[0], args[1], args[2], args[3], args[4], args[5]);
            }
            static void GeneratePDFReports(string reportClassName, string FileDirectory, string FileName, string SQLStatement, string FileDir, string Library)
            {
                //
                // GeneratePDFReports body
                //
            }
    }
    }

    With best regards, Yasser Zamani
    • Proposed as answer by SamAgain Monday, December 28, 2009 9:46 AM
    • Marked as answer by RakeshRao Monday, December 28, 2009 11:46 PM
    Thursday, December 24, 2009 8:40 PM

All replies

  • Create a console application and pass arguments to it.
    With best regards, Yasser Zamani
    Wednesday, December 23, 2009 7:28 PM
  • Or you can use C# script.

    -Karel
    Wednesday, December 23, 2009 7:36 PM
    Moderator
  • Thanks Yasser ..But in this scenario i am unable to figure it out how to do that....Do you have any examples in this scenario...I didnot find anything in msdn or google 
    Thursday, December 24, 2009 1:42 AM
  • Hi Karel how to use C# script in this scenario
    Thursday, December 24, 2009 1:43 AM
  • Re Yasser's advice: Read documentation about parsing arguments to C# applications from command line - http://msdn.microsoft.com/en-us/library/cb20e19t.aspx.
    Re: How to use C# script: Search the internet - for example: http://www.codeproject.com/KB/cs/cs-script_for_cp.aspx

    If you cannot make GeneratePDFReports your Main method, then let your Main method take class/method name as parameter and use Reflection to run it from your Main method. That is however quite advanced coding.

    -Karel

    Thursday, December 24, 2009 2:40 AM
    Moderator
  • Thanks Yasser ..But in this scenario i am unable to figure it out how to do that....Do you have any examples in this scenario...I didnot find anything in msdn or google 

    rem GenerationOfFiles1.bat file
    @ECHO off
    CD  C:\MF
    rem SETTING THE LOCATION OF THE C# COMPILER
    SET framework=%SystemRoot%\Microsoft.NET\Framework\v1.1.4322
    rem COMPILING THE CS FILE
    %framework%\csc  GenerationOfFiles1.cs
    rem EXECUTING  THE C# APPLICATION
    GenerationOfFiles1 "A" "B" "C" "D" "E" "F"

    ========================================================
    //GenerationOfFiles1.cs file
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GenerationOfFiles1
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length != 6)
                {
                    Console.Write("Usage: GenerationOfFiles1 string reportClassName, string FileDirectory, string FileName, string SQLStatement, string FileDir, string Library");
                    return;
                }
                GeneratePDFReports(args[0], args[1], args[2], args[3], args[4], args[5]);
            }
            static void GeneratePDFReports(string reportClassName, string FileDirectory, string FileName, string SQLStatement, string FileDir, string Library)
            {
                //
                // GeneratePDFReports body
                //
            }
    }
    }

    With best regards, Yasser Zamani
    • Proposed as answer by SamAgain Monday, December 28, 2009 9:46 AM
    • Marked as answer by RakeshRao Monday, December 28, 2009 11:46 PM
    Thursday, December 24, 2009 8:40 PM
  • Thanks very much yasser and karel...I am able to do it
    Monday, December 28, 2009 11:48 PM