您的位置:首页 > 编程语言 > C#

Inside c#_Chapter1(02)

2004-11-21 22:03 507 查看
“Hello, World”—The Command-Line Version[/b]

命令行版本的“[/b]Hello, World[/b]”[/b][/b]

At this point, create a new file in a standard text editor. For purposes of this demo, I’m using the standard Notepad application because it’s available on all standard installations of Windows. Once you’ve created a new file, type in the following code. (Don’t worry about what each line does at this point—we’ll be covering that soon enough.)

现在我们使用标准文本编辑器创建一个新文件。为完成这一示例程序,推荐使用Windows 记事本程序,因为在任一个版本的Windows中都包含这一应用程序。一旦你建立了一个新文件,输入下面的代码。(不要对下面的每一行代码完成什么工作感到疑惑,稍候我会给出清晰的解释)

namespace InsideCSharp

{

class HelloWorldConsoleApp

{

static void Main()

{

System.Console.WriteLine("Hello, World");

}

}

}

Using the Command-Line Compiler[/b]

使用命令行编译器[/b][/b]

To see immediately if your environment is set up properly, we’ll hold off on explaining the code and first attempt to build and run this application. Therefore, when you’ve finished typing in the code, save the file as HelloWorldConsole and open a command window. Then run the following command from the command prompt:

如果你的.NET环境安装正确,你会马上看到结果,我们先尝试构建和运行应用程序稍候再解释代码。所以你输入完代码后将文件以HelloWorldConsole(扩展名为.cs)文件名存盘,接着打开命令提示符窗口。然后输入如下命令:

csc helloworldconsole.cs[/i]

[/i]

Here you’re simply invoking the C# compiler (csc.exe) and passing it the name of the file to compile. If everything goes well, you should see results that resemble Figure 1-1.[/b]

这里我们简单的调用C#的编译器并传送文件的名字给它以完成编译。如果一切正常,你将看到如插图1-1所示的结果。




Figure 1-1[/b]

When using the command-line compiler, if your application builds correctly, you’ll simply see the copyright information of the C# compiler with no errors listed.[/i]

插图[/b]1-1[/b]

当使用命令行编译器时,如果正确构建了你的应用程序,你只会看到[/i]C#[/i]编译器提供的不包含错误列表的版权信息[/i][/i]

If your application compiled, you’ve not only written your first .NET application in C#, but your system is properly configured to build every demo application in this book! However, if you received the familiar “command not found” error, the command interpreter couldn’t locate the csc.exe application. To remedy this, you need make only one slight change to your Windows environment—more specifically, you make the change to a Path environment variable.

如果你的应用程序编译完成,你不仅仅是编写了第一个C#应用程序,而且说明你的系统已经配置好可以完成这本书中所有示例应用程序的编译!但是,如果你看到了熟悉的“Command not found”(操作系统版本不同,提示信息不同)错误信息,说明命令解释器不能定位csc.exe应用程序。修正这一错误你只需要对Windows环境做轻微的改动,明确地说你需要修改Path环境变量。

To do so, open the System Control Panel applet shown in Figure 1-2 and click the Advanced tab.

要这样做,请打开如插图1-2所示的系统属性面板,单击“高级”选项卡。



Figure 1-2[/b]

Environment variables can be set via the System Control Panel applet so that they’re applicable to any newly opened command session.[/i]

On the Advanced tab, click the Environment Variables button to open the Environment Variables dialog box shown in Figure 1-3. This dialog box displays both the user environment variables for the currently logged on user as well as the system-level variables—those pertaining to any logged on user.

在高级选项卡内,单击环境变量[/b]按钮已打开环境变量对话框(如插图1-3所示)对话框显示当前登录用户的环境变量和针对所有登录本机的用户的系统级的环境变量。



Figure 1-3[/i][/b]

Environment variables tell Windows—and its applications—where to find certain types of information.[/i]

Now locate the Path variable in the System Variables list box, select it, and then click the Edit button to display the Edit System Variable dialog box shown in Figure 1-4. Finally, append the following text to the end of the Path variable’s value:

现在在系统变量列表框中选定Path环境变量,然后单击编辑按钮显示编辑系统变量对话框(如插图1-4所示)将下面的文本添加到变量现有值的末尾:

;C:\Program Files\Microsoft Visual Studio
.NET\FrameworkSDK\Bin;c:\winnt\microsoft.net\framework\v1.0.3705;
C:\Program Files\Microsoft Visual Studio .NET\Vc7\bin;
C:\Program Files\Common Files\Microsoft Shared\VSA\7.0\VsaEnv;
The version number in the Path is the value that’s being used in the build that I’m writing this book with and might be different for your particular installation at the time that you read this book.

上面路径中所出现的版本号是我所使用的,也许会和你的系统中安装的版本不同。



Figure 1-4[/i][/b]

The Edit System Variable dialog box.[/i]

Once you’ve set the Path variable, you’ll need to open a new command prompt and retype the command to compile the HelloWorldConsole application. At this point, everything should build and you should now see that the executable named HelloWorldConsole.exe has been created. Run the program to see the output, shown in Figure 1-5.

一旦你设定好Path变量,打开一个命令提示符窗口重新尝试编译HelloWorldConsole应用程序。现在所有的都将被构建你还可以看到一个HelloWorldConsole.exe的可执行文件被创建,运行它并查看结果。(如插图1-5所示)



Figure 1-5[/i][/b]

The output of the HelloWorldConsole program.[/i]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: