文章目录

  • 前言
  • 一、程序效果展示?
    • 1.生成的文件名
    • 2.不带参数执行将提示帮助
    • 3.这是一个正常Program.cs文件
    • 4.接下来,我们就用这个程序给它加上行号
    • 5.那么,如何进行反转呢,把有行号处理成没行
  • 二、程序完整源代码如下:
  • 写在题后


前言

作为耕耘多年的码农,不管是编书,还是写作、制作PPT,经常要给源代码加上行号,以此增加代码的可读性。这样的场景也经常会产生反转,有时需要把带有行号的代码去掉行号,直接复制到编程环境进行调试。


提示:以下是本篇文章正文内容,下面案例可供参考

一、程序效果展示?

1.生成的文件名

程序生成后是一个命令行程序,文件名:WriteLine.exe

2.不带参数执行将提示帮助

3.这是一个正常Program.cs文件

4.接下来,我们就用这个程序给它加上行号

writelines program.cs p.cs

注意源文件program.cs没有改变,是生成了一个新的文件p.cs,打开它看看就知道了。

5.那么,如何进行反转呢,把有行号处理成没行


p.cs是带行号的文件,生成的新文件p1.cs是不带行号的,后面的参数:/del含义就是删除行号。
p1.cs文件内容跟原来的program.cs文件内容是完全一样的,不带行号,请读者自行验证。

二、程序完整源代码如下:

代码如下(示例):

using System;
using System.IO;
namespace WriteLines
{
  class Program
    {
        static void Main(string[] args)
        {

            if (args.Length == 3)
            {
                if (args[2] == "/del")
                {
                    string path1 = Environment.CurrentDirectory + "\\" + args[0];
                    string path2 = Environment.CurrentDirectory + "\\" + args[1];
                    if (File.Exists(path1))
                    //if (File.Exists(path1) && !File.Exists(path2))
                    {
                        StreamReader sr = new StreamReader(path1);
                        StreamWriter sw = new StreamWriter(path2);
                        string line = sr.ReadLine();
                        
                        while (line != null)
                        {
                            int a;
                            if (!int.TryParse(line.Substring(0, 1),out a))
                            {
                                Console.WriteLine("源文件本身没有行号。");
                                return;
                            }
                            line = line.Substring(3);
                           

                            sw.WriteLine(line);
                            line = sr.ReadLine();
                        }
                        sr.Close();
                        sw.Close();
                    }
                    Console.WriteLine("文件处理完毕!");
                    return;
                }
            }
            else if (args.Length == 2)
            {
                string path1 = Environment.CurrentDirectory + "\\" + args[0];
                string path2 = Environment.CurrentDirectory + "\\" + args[1];
                if (File.Exists(path1))
                //if (File.Exists(path1) && !File.Exists(path2))
                {
                    StreamReader sr = new StreamReader(path1);
                    StreamWriter sw = new StreamWriter(path2);
                    string line = sr.ReadLine();
                    int i = 0;
                    while (line != null)
                    {
                        string strI = i.ToString();
                        if (strI.Length == 1)
                        {
                            strI = "0" + strI;
                        }

                        sw.WriteLine(strI + "\t" + line);
                        line = sr.ReadLine();
                        i++;
                    }
                    sr.Close();
                    sw.Close();
                }
                Console.WriteLine("文件处理完毕!");
            }
            else
            {
                Console.WriteLine("参数不足!");
                Console.WriteLine("Usage:");
                Console.WriteLine("Writelines filename1 filename2 [/del]");
                Console.WriteLine("filename1,filename2不带路径,所有生成文件都在当前路径下!");
            }
        }
    }
}

写在题后

对源代码有疑问的读者可与本人交流,可以提供项目文档。当然更加希望有创意的读者给我建议,不断完善这个程序。

更多推荐

给源代码加个行号吧