How to build C# executable/program

dr761
dr761 used Ask the Experts™
on
I've written a Visual Studio C# web application.  When it runs, it puts a submit button on the screen.  When the user clicks the submit button it runs some processing which does all of the following:
Reads and writes a sql database
Performs lookups to Active Directory
Writes a log file to disk

I'd like to convert this to an executable that can be run from the command line or called by a scheduler.  If possible, I'd like it to take one parameter which would be the path and name of the log file.  Output or a return value are optional.

While it would be preferable that the exe file contain all necessary files, I can copy supporting files along with the exe when it is distributed/installed.

I'm open to all suggestions/ideas.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
For this you have to prepare console application if you dont need any gui.

But in your case it needs a button to be placed. So I recommend you go for Win App. Also After you application preparation. Right click on Solution in the solution explorer. and click on Build. That Will build the files under Solution Dir > Startup Project Dir > bin > Release.

You can use those exe file placed in the release folder can be used for your scheduling. If you make this as console application it would be more comfortable to use if you dont need to perform gui related operations.
HainKurtSr. System Analyst

Commented:
and here a pice from me ;)

if you run your app as:

myReport.exe a=1 name=HainKurt p=c:\dev\logs

in app, you can use

string name = getComandLineArgument("name", "NA");

where NA is default value if none is passed...
        private string getComandLineArgument(string argName, string defValue)
        {
            foreach (string arg in Environment.GetCommandLineArgs())
            {
                string[] argKey = argName.Split('=');
                if (argKey.Length == 2)
                {
                    string aName = argName.Split('=')[0];
                    string aValue = argName.Split('=')[1];
                    if (aName.ToLower() == argName.ToLower()) return aValue;
                }
            }
            return defValue;
        }

Open in new window

HainKurtSr. System Analyst

Commented:
also another piece if you go with console app

use like

Log("Application started");
...
Log("Connected to db : " + dbName);
        private void Log(string msg)
        {
            Console.Write(DateTime.Now + " : ");
            Console.WriteLine(msg);
        }

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

HainKurtSr. System Analyst

Commented:
also if you go with console app, you can run your app like this

myReport.exe a=1 name=HainKurt p=c:\dev\logs > c:\temp\myReport.log
or
myReport.exe a=1 name=HainKurt p=c:\dev\logs >> c:\temp\myReport.log

to save/append output to log file...
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi dr761;

You can use the Console Application template when creating the application in Visual Studio seeming a GUI is not needed because running the application would be like pressing the button on a GUI itself. If you need any help please just ask.

Fernando
HainKurtSr. System Analyst

Commented:
"command line or called by a scheduler" means console app, no GUI I guess...

Author

Commented:
I don't need a button, I just did that for the web app.

I don't think that I can use a console app because when I tried to create one the "using System.DirectoryServices" which is necessary for accessing Active Directory isn't available.

When I open a new project in Visual Studio 2008, I need to know what kind of project to open and then what to do with the code I have in my web application.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi dr761;

you need to add a reference to the project to System.DirectoryServices to get access to the objects in that namespace then place the using statement in your code file.

using System.DirectoryServices;

Fernando
Sr. System Analyst
Commented:
New Project, Visual basic / C#, Windows, Console Application
Fernando SotoRetired
Distinguished Expert 2017
Commented:
Hi dr761;

To your statement, "When I open a new project in Visual Studio 2008, I need to know what kind of project to open", Open a Console Application.

In Solution Explorer right click on the project name and then click on Add Reference in the context menu. When the Add Reference window opens find System.DirectoryServices in the list in the .Net tab, select it and click on OK button. Now add your using statement.

To your statement, "what to do with the code I have in my web application.", that is difficult to say without seeing it because we don't not how you structured you code.

Fernando

Author

Commented:
Sorry for the delay on this, other work got in the way.  I haven't actually had a chance to complete the project but since you've both been helpful I'm going to spit the points.  Thanks for your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial