[英]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. 使用
VisualStudio
可以轻松地为控制台,窗体等创建应用程序类型,但是我看不到命令行应用程序的选项。 I intend to install the small program below as a simple .exe in c:\\windows\\system32. 我打算将以下小程序作为简单的.exe安装在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.
如果我打开命令终端,然后将CD打开到VS项目的bin \\ debug目录,则可以键入DateTime并获得不错的输出。 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.
但是,如果我将DateTime.exe复制到c:\\ windows \\ system32并打开另一个命令终端,则由于.Net Shim错误,命令DateTime会给出一个错误,指出无法启动该应用程序。
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 ? 我应该从c:\\ windows \\ system32的bin \\ debug目录中安装更多文件吗?
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. 注意:上面的命令行应用程序的原因是系统命令
Date/T
和Time/T
不提供秒输出。 My app shows eg 2015-07-13T10:58:29.7329261+02:00 (, and you can get other formats with an argument, see previous edits of this question) 我的应用程序显示例如2015-07-13T10:58:29.7329261 + 02:00(并且您可以获取带有参数的其他格式,请参阅此问题的先前编辑)
Console project is a command line project. 控制台项目是命令行项目。
You can use the args [] to track command line parameters. 您可以使用args []来跟踪命令行参数。
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. 您的可执行文件可能具有依赖性:您可以使用混淆器或其他工具将其打包到.exe中,因此不需要外部文件。
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. 或者,将项目安装到其自己的文件夹中,然后修改PATH变量以包含目录的路径-这将使您可以从任何文件夹运行.exe,就像可以在任何文件夹中运行“ dir”一样。
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. 我不清楚您的
DateTime
问题是什么,但是您可以使用.ToString()
重载来调整格式,并使用.Parse()
从多种格式中解释日期。
var dt = new DateTime();
dt.ToString("dd/MM/yyyy hh:mm:ss");
As advised by SLC, I tried to write this app in C++. 根据SLC的建议,我尝试用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.
事实证明,VisualStudio为C ++控制台应用程序提供了选择:使用CLR或Win32,其中CLR代表dot-Net Framework。 Indeed the Win32 project builds as a simple .EXE file that can be installed in any directory mentioned in %PATH%, eg, C:\\Windows\\System32\\ .
实际上,Win32项目构建为一个简单的.EXE文件,可以将其安装在%PATH%中提到的任何目录中,例如C:\\ Windows \\ System32 \\。
VisualStudio does not offer this choice for C# Console apps. VisualStudio不为C#控制台应用程序提供此选择。
Unfortunately, C++ is a step backwards from C#: it is more complex. 不幸的是,C ++比C#落后了一步:它更加复杂。 It should run faster for certain types of apps, but cost more time to develop.
对于某些类型的应用程序,它应该运行得更快,但会花费更多的开发时间。 Compare this C++ code with the C# source above:
将此C ++代码与上面的C#源进行比较:
#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%. 如前所述,以上C ++代码作为
Win32 Console project
,将作为%PATH%目录中的单个.EXE文件运行。
For completeness, here is a nicer source code in C++, as a CLR Console project
, ie, 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%. 为了完整起见,这是C ++中更好的源代码,作为
CLR Console project
,即使用.Net Framework,它的编码速度与C#相当,但也不会作为%。目录中的单个.EXE文件运行路径%。
#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, eg: 输出例如:
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%. 上面的应用程序也已通过
PowerShell
进行了测试,但是基于CLR / .Net的应用程序也无法从%PATH%运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.