stringreplace

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace stringreplace
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                string helpMessage =
         @"Replace string in a file with another string.

STRINGREPLACE [sourcefile] [sourcestring] [targetstring] [targetfile]

Parameter:It have 4 parameter: 
[sourcefile]    Import file name for replace
[sourcestring]  Import source string
[targetstring]  Import replace string
[targetfile]    Export file name";
                Console.WriteLine(helpMessage);
                return;
            }
            string sfile, sstr, tstr, tfile;
            sfile = args[0];
            sstr = args[1];
            tstr = args[2];
            tfile = args[3];

            if (!File.Exists(sfile))
            {
                string errorMessage = string.Empty;
                errorMessage = string.Format("Error:Sourcefile[{0}] is not exist", sfile);
                Console.WriteLine(errorMessage);
                return;
            }

            if (File.Exists(sfile))
            {
                FileStream fs = File.OpenRead(sfile);
                StreamReader sr = new StreamReader(fs);
                string content = sr.ReadToEnd();
                string replacedcontent = content.Replace(sstr, tstr);
                sr.Close();
                fs.Close();

                StreamWriter sw = File.CreateText(tfile);
                sw.Write(replacedcontent);
                sw.Close();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值