Roland
Roland

Reputation: 5234

How to create a command line application

With VisualStudio it is easy to create application types for Console, Forms, etc, but I see no option for a command line application. I intend to install the small program below as a simple .exe in c:\windows\system32. If I open a command terminal and CD to the bin\debug dir of the VS project, I can type DateTime and get nice output. However, if I copy the DateTime.exe to c:\windows\system32 and open another command terminal, the command DateTime gives an error saying that the application could not be started, because of .Net Shim errors.

Should I do something different to create a commandline application rather than a Console app?

Should I install more files from the bin\debug directory in c:\windows\system32 ?

using System;

namespace DateTime
{
    class Program
    {
        static void Main()
        {
                Console.Write(System.DateTime.Now.ToString("o"));
        }
    }
}

NB: reason for the above command line app is that the system commands Date/T and Time/T do not provide seconds output. My app shows e.g. 2015-07-13T10:58:29.7329261+02:00 (, and you can get other formats with an argument, see previous edits of this question)

Upvotes: 0

Views: 10753

Answers (3)

Gopalakrishnan R
Gopalakrishnan R

Reputation: 41

There is a simple NuGet package that helps you to create your own command-line application without handling many things line parsing the arguments and mapping the types etc.,

https://www.nuget.org/packages/CommandLineTool/

Upvotes: 0

Roland
Roland

Reputation: 5234

As advised by SLC, I tried to write this app in C++. It turns out that VisualStudio offers a choice for C++ Console apps: with CLR, or as Win32, where CLR stands for the dot-Net Framework. Indeed the Win32 project builds as a simple .EXE file that can be installed in any directory mentioned in %PATH%, e.g., C:\Windows\System32\ .

VisualStudio does not offer this choice for C# Console apps.

Unfortunately, C++ is a step backwards from C#: it is more complex. It should run faster for certain types of apps, but cost more time to develop. Compare this C++ code with the C# source above:

#include "stdafx.h"
#include <iostream>
#include <ctime>

int main()
{
    // get current time
    time_t time_now = time(0);
    // convert to local time struct
    struct tm tstruct;
    localtime_s(&tstruct, &time_now);
    // format to string
    char str_time[80];
    strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tstruct);
    // to stdout
    std::cout << str_time << std::endl;
    return 0;
}

As said, the above C++ code, as a Win32 Console project, will run as a single .EXE file from a directory in %PATH%.

For completeness, here is a nicer source code in C++, as a CLR Console project, i.e., with the .Net Framework, that will code comparably fast as C#, but will also NOT run as a single .EXE file from a directory in %PATH%.

#include "stdafx.h"
using namespace System;
int main()
{
    Console::WriteLine(DateTime::Now);
    Console::WriteLine(DateTime::Now.ToString(L"o"));
    Console::WriteLine(DateTime::Now.ToString(L"yyyy-MM-dd HH:mm:ss"));
    return 0;
 }

Output is, e.g.:

14-7-2015 16:23:27
2015-07-14T16:23:27.6978497+02:00
2015-07-14 16:23:27

The above apps are also tested from PowerShell, but then the CLR/.Net based apps can also not be run from %PATH%.

Upvotes: 0

NibblyPig
NibblyPig

Reputation: 52942

Console project is a command line project.

You can use the args [] to track command line parameters.

Your executable may have dependencies: You can use something like obfuscator or another tool to package them into the .exe so you don't need external files.

Alternatively, install your project into its own folder, and modify the PATH variable to include the path to the directory - this will let you run your .exe from any folder, much like you can run 'dir' in any folder.

I'm not clear what your DateTime problem is, but you can use the .ToString() overload to adjust the formatting, and .Parse() to interpret the date from a variety of formats.

var dt = new DateTime();
dt.ToString("dd/MM/yyyy hh:mm:ss");

Upvotes: 4

Related Questions