Console app with parms -> dll app with parms

Hi
I have a C# console application, that I will call from a website (ASP.NET).
Is it possible to convert the console app to a Class/dll and then call it.
And how?

The console app have parameters on.
conceptdataAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Well, because you are now using your Console Application as code instead of a standalone program, the relative paths you are using are relative to the location of your web app, not the location of the Console Application. So something like "../conf/userconfig.xml" is saying:

   Go up one directory from the web application's directory, find the conf directory, find userconfig.xml

Is this where that xml file is located in relation to the directory of the website?
0
 
käµfm³d 👽Commented:
Converting to a dll is possible. When you add the reference to the dll, just call the appropriate function with the required parameters.

My question to you would be: Is there a reason you need it to be a dll? You can start an executable from your site by using System.Diagnostics.Process.Start("C:\path\filename.exe"). You would most likely need to add the executable to your website folder in order for the ASPNET user to execute it; otherwise you will have to grant permissions to the ASPNET user on the folder and executable containing your program.
0
 
conceptdataAuthor Commented:
Thanks.
I started to use the Process.Start, and a .exe file.
BUT, then I read that it could be an unstable solution.

I will try it
What about parameters ?
my local batch file looks like this :
nfop.exe -c ..\conf\userconfig.xml -fo ..\off.xsl.fo -pdf ..\off.pdf
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
tcullerCommented:
Just throw your "parameters" in next to the file in the Start method. For example, see the code below.

Do you need to actually start this process, or can you just use its methods instead? You can add a reference to an EXE just as well as a DLL, if the EXE was programmed in a .NET-Compliant language using the CLR.
System.Diagnostics.Process.Start("nfop.exe",
"-c ..\conf\userconfig.xml -fo ..\off.xsl.fo -pdf ..\off.pdf");

Open in new window

0
 
conceptdataAuthor Commented:
I'm a little bit newbie into this.
What do you mean by :
-------------
You can add a reference to an EXE just as well as a DLL, if the EXE was programmed in a .NET-Compliant language using the CLR.
-------------
Can you explain or better give an example - THANKS

0
 
käµfm³d 👽Commented:
If your program was written in C#, VB.NET, C++ .NET, etc..  then you can add a reference to it--just like you add references to dll's. Just go to Project->Add Reference->Browse Tab and then locate your executable file and click Ok.

I didn't think about doing it that way. Good call tculler.
0
 
conceptdataAuthor Commented:
Okay thanks - But how do I run the "reference" (exe file) then ?
0
 
käµfm³d 👽Commented:
You won't be running the file at that point--at least not in the sense of running an executable through explorer. You can call all the public methods that you declared in your executable--just like when you reference a dll, you can call the public functions available in that dll.

Look at the following two applications. ConsoleApplication2 has a public class "A_Class" that has a public method "Add_1_and_2()". You can build/run this application as a standalone executable.

ConsoleApplication3 has a reference to ConsoleApplication2. As such, I can declare an "A_Class" object and call its method(s).
//////////////////////////////////////
//  App 1
//////////////////////////////////////
 
using System;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            A_Class ac = new A_Class();
 
            Console.WriteLine(ac.Add_1_and_2());
            Console.ReadKey();
        }
    }
 
    public class A_Class
    {
        private const int ONE = 1;
        private const int TWO = 2;
 
        public int Add_1_and_2()
        {
            return ONE + TWO;
        }
    }
}
 
//////////////////////////////////////
//  App 2
//////////////////////////////////////
 
using System;
 
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleApplication2.A_Class ac = new ConsoleApplication2.A_Class();
 
            Console.WriteLine(ac.Add_1_and_2());
            Console.ReadKey();
        }
    }
}

Open in new window

0
 
käµfm³d 👽Commented:
Additionally, if you really wanted to be confusing to coders that follow behind you (and I am, of course, not suggesting you do this; rather I am demonstrating a concept), you could call the static method Main of ConsoleApplication2 from ConsoleApplication3. To do this, however, you would have to, within ConsoleApplication2, mark the class Program as public and the static method Main as public. Then the following could be done in ConsoleApplication3:
using System;
 
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleApplication2.Program.Main(args);  // We marked "Program" and "Main" as public in ConsoleApplication2
 
            Console.ReadKey();
        }
    }
}

Open in new window

0
 
conceptdataAuthor Commented:
I'm a little bit lost here..

my nFopApp look like Code Snippet
using System;
 
namespace NFopApp
{
   
 
    
    public class NFopApp
    {
        public static void PrintUsage()
        {
            Console.WriteLine(GetUsage());
        }
 
        public static string GetUsage()
        {
            System.Text.StringBuilder usageBuffer = new System.Text.StringBuilder();
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("nFop [options] [-fo|-xml] <infile> [-xsl file] [-foout | -pdf] <outfile>");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("[OPTIONS]");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("-c cfg.xml        use additional configuration file cfg.xml");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("(you can set NFOP_USER_CONFIG_PATH environment variable and override it with -c parameter)");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("[INPUT]");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("infile            xsl:fo input file (the same as the next)");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("-fo  infile       xsl:fo input file");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("-xml infile       xml input file, must be used together with -xsl");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("-xsl stylesheet   xslt stylesheet");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("[OUTPUT]");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("-pdf outfile      input will be rendered as pdf file (outfile req'd)");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("-foout outfile    input will be rendered as fo file (outfile req'd)");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("[Examples]");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("nFop foo.fo foo.pdf");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("nFop -fo foo.fo -pdf foo.pdf (does the same as the previous line)");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("nFop -xsl foo.xsl -xml foo.xml -pdf foo.pdf");
            usageBuffer.AppendFormat(Environment.NewLine);
            usageBuffer.AppendFormat("nFop -xsl foo.xsl -xml foo.xml -foout foo.fo -pdf foo.pdf");
            usageBuffer.AppendFormat(Environment.NewLine);
 
            return usageBuffer.ToString();
        }
 
        public static bool CheckParameters(CommandLineArguments cmdLineArgs)
        {
            if (cmdLineArgs.HasArguments)
            {
                switch (cmdLineArgs.Arguments.Count)
                {
                    case 2: if (cmdLineArgs.HasFO  && cmdLineArgs.HasPDF) return true; break;
                    case 3: if (cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasPDF) return true; break;
                    case 4: if (cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasFOOUT && cmdLineArgs.HasPDF) return true; break;
                }
            }
 
            return false;
        }
 
 
        static void Main(string[] args)
        {
            int ExitCode = 0;
 
            try
            {
                CommandLineArguments cmdLineArgs = new CommandLineArguments(args);
 
                if (CheckParameters(cmdLineArgs))
                {
                    switch (cmdLineArgs.Arguments.Count)
                    {
                        case 2:
 
                            if (cmdLineArgs.HasUSERCONFIG && cmdLineArgs.HasFO && cmdLineArgs.HasPDF)
                            {
                                Fop.Net.NFop.Create_PDF_from_FO_with_CONFIG(cmdLineArgs.FO, cmdLineArgs.PDF, cmdLineArgs.USERCONFIG);
                            }
                            else if (cmdLineArgs.HasUSERCONFIG == false && cmdLineArgs.HasFO && cmdLineArgs.HasPDF)
                            {
                                Fop.Net.NFop.Create_PDF_from_FO(cmdLineArgs.FO, cmdLineArgs.PDF);
                            }
 
                            break;
                        case 3:
 
                            if (cmdLineArgs.HasUSERCONFIG && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasPDF)
                            {
                                Fop.Net.NFop.Create_PDF_from_XML_XSL_with_CONFIG(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.PDF, cmdLineArgs.USERCONFIG);
                            }
                            else if (cmdLineArgs.HasUSERCONFIG == false && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasPDF)
                            {
                                Fop.Net.NFop.Create_PDF_from_XML_XSL(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.PDF);
                            }
 
                            break;
                        case 4:
 
                            if (cmdLineArgs.HasUSERCONFIG && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasFOOUT && cmdLineArgs.HasPDF)
                            {
                                Fop.Net.NFop.Create_PDF_and_FO_from_XML_XSL_with_CONFIG(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.FOOUT, cmdLineArgs.PDF, cmdLineArgs.USERCONFIG);
                            }
                            else if (cmdLineArgs.HasUSERCONFIG == false && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasFOOUT && cmdLineArgs.HasPDF)
                            {
                                Fop.Net.NFop.Create_PDF_and_FO_from_XML_XSL(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.FOOUT, cmdLineArgs.PDF);
                            }
 
                            break;
                    }
                }
                else
                {
                    PrintUsage();
                }
            }
            catch (System.IO.FileNotFoundException fileNotFoundException)
            {
                ExitCode = 1;
                Console.WriteLine(fileNotFoundException.Message);
            }
            catch (CommandLineArgumentException commandLineArgumentException)
            {
                ExitCode = 1;
                Console.WriteLine("{0}:\t{1}", commandLineArgumentException.Message, commandLineArgumentException.ParamName);
            }
            catch (Exception exception)
            {
                ExitCode = 1;
                Console.WriteLine("{0}{1}{2}", exception.Message, Environment.NewLine, exception.StackTrace);
            }
 
            Environment.Exit(ExitCode);
        }
    }
}

Open in new window

0
 
käµfm³d 👽Commented:
I'm a little lost too....   What is this code and what are you trying to do with it :)
0
 
conceptdataAuthor Commented:
I have attached the solution.

The fil KJ_Build.bat makes a PDF-file from xML and xsl files...
The folder nfop_fw2\nfop\nfopapp\bin\debug\ have the nfop.exe file that I imported into another C# asp.net solution, and then I should "run" the reference like the KJ_Build.bat....

Any idea ??
0
 
conceptdataAuthor Commented:
UPS, here is the attached file
test1.txt
0
 
conceptdataAuthor Commented:
rename attached file to *****.rar and unrar
0
 
käµfm³d 👽Commented:
Your Main() method in NFopApp class will need to be marked as public in order to call it from external code. Once you make that change (and add a reference to the executable), you can call it as demonstrated in the attached image (click to enlarge).
untitled1.JPG
0
 
conceptdataAuthor Commented:
Okay, where do I put my parameters :

-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf
0
 
conceptdataAuthor Commented:
One more thing.
I have to call til reference from a aspx website... maybe from the C# code behind.
0
 
käµfm³d 👽Commented:
For parameters, you could do something like in the snippet. Also, yes, you can call this function from within the code-behind.
NFopApp.NFopApp.Main("-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf".Split(new char[] {' '});

Open in new window

0
 
conceptdataAuthor Commented:
Something like this ??

But do it call the reference / exe file ?
namespace nFopWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(string[] args)
        {
            
            NFopApp.NFopApp.Main("-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf".Split(new char[] {' '}));
        }
        }
 
    }

Open in new window

0
 
käµfm³d 👽Commented:
Yes, like that. You will not see a console window because you are not starting a process. You are actually calling the methods contained within NFopApp as if there were in the website project themselves. Since Main() is public and static, you don't have to create an object of type NFopApp in order to call the function Main().
0
 
conceptdataAuthor Commented:
Okay, I've tried it, but it is no working for me.

BUT, maybe it is possible that i don't manage to put the right directory for the file in the parameter ..

NFopApp.NFopApp.Main("-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf".Split(new char[] {' '}));

Can I use server.mappath og something here ...
.
0
 
conceptdataAuthor Commented:
Thanks - now it works
0
All Courses

From novice to tech pro — start learning today.