如何C#命令行参数指定?
来源:百度知道 编辑:UC知道 时间:2024/05/27 16:36:58
using System;
using System.IO;
class test
{
public static void Main(string[] args)
{
string path="" ;
if (args.Length == 0)
{
Console.WriteLine("请指定文件名。");
return;
}
else
{
path =args[0];
}
//写入文件
StreamWriter sw = null;
try
{
sw = new StreamWriter(path, true);
sw.WriteLine(DateTime.Now.ToString());
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
}
finally
{
//关闭文件按
sw.Close();
给你写了一个例子,参考下。
********************
public static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请指定文件名。");
String str = Console.ReadLine();
FileInfo fi = new FileInfo(str);
if (!fi.Exists)
{
Console.WriteLine("文件路径不正确!请重新指定");
}
Console.WriteLine("文件路径正确,请输入填充文字");
String words = Console.ReadLine();
StreamWriter sw = null;
sw = new StreamWriter(str);
sw.Write(words);
Console.WriteLine("文件保存成功!");
sw.Close();
Console.WriteLine("退出请输入exit,读取文件信息输入read");
String str2=Console.ReadLine();