《21天學通C#》將寫的程式碼原封不動的在控制檯顯示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 第二天課程課後習題
{
class Program
{
static void Main(string[] args)
{
try
{
int ctr = 0;
if(args.Length<=0)
{
Console.WriteLine("Format:ListIT filename");
}
else
{
FileStream f = new FileStream(args[0],FileMode.Open);
try
{
StreamReader t = new StreamReader(f);
string line;
while((line=t.ReadLine())!=null)
{
ctr++;
Console.WriteLine("{0}:{1}", ctr, line);
}
f.Close();
}
finally
{
f.Close();
}
}
}
catch(System.IO.FileNotFoundException)
{
Console.WriteLine("ListIT could not find the file", args[0]);
}
catch(Exception e)
{
Console.WriteLine("Exception:{0}\n\n", e);
}
Console.WriteLine("請按任意鍵退出程式!");
Console.ReadKey();
}
}
}
然後開啟VS 2017開發人員命令提示符,切換到Program.cs所在目錄,輸入csc Program.cs
然後再次輸入Program Program.cs
如圖所示:
會顯示出所寫程式碼