去除代码行号的一个小程序(控制台版本)

此工具提供了一种简单的方法来移除文本文件中的行号,支持单个文件或整个目录的批量处理,适用于.NET Framework 1.1和2.0。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

清风竹林发布了去除代码行号的一个小程序,确实方便大家收集一些文章代码,但个人认为象这样的小东东,要使广大网友能拿来就用,用.Net 2.0做成WinForm,有点贵族化了,于是动手整出个平民化的控制台版本,可以清除指定的文本文件,也可以对指定目录进行批量清除,希望对大家有点作用。以下代码在.Net Framework1.1与.Net Framework2.0均可运行。

  1 None.gif using  System;
  2 None.gif using  System.IO;
  3 None.gif using  System.Text;
  4 None.gif
  5 None.gif namespace  Ycweb
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// Summary description for Class1.
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    class CLN
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 13InBlock.gif        /// The main entry point for the application.
 14ExpandedSubBlockEnd.gif        /// </summary>

 15InBlock.gif        [STAThread]
 16InBlock.gif        static void Main(string[] args)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            //
 19InBlock.gif            // TODO: Add code to start application here
 20InBlock.gif            //
 21InBlock.gif            if(args.Length<1)
 22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 23InBlock.gif                Console.WriteLine("用法:\n\r\t CLN YourFile.TXT|YourDirectory");
 24ExpandedSubBlockEnd.gif            }

 25InBlock.gif            else
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 27InBlock.gif                string tmpArg=args[0];
 28InBlock.gif
 29InBlock.gif                if(tmpArg.StartsWith("/"|| tmpArg.StartsWith("?"))
 30ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 31InBlock.gif                    Console.WriteLine("用法:\n\r\t CLN YourFile.TXT|YourDirectory");
 32ExpandedSubBlockEnd.gif                }

 33InBlock.gif                else
 34ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 35InBlock.gif                    //假定用户提供的参数为目录,则先判断目录是否存在,如果存在则遍历该目录下的所有文本文件并清除行号
 36InBlock.gif                    if(System.IO.Directory.Exists(tmpArg))
 37ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 38ContractedSubBlock.gifExpandedSubBlockStart.gif                        Clear Line Numbers For Files In The Directory#region Clear Line Numbers For Files In The Directory
 39InBlock.gif                        DirectoryInfo di=new DirectoryInfo(tmpArg);
 40InBlock.gif                        FileInfo[] txtFileInfo = di.GetFiles("*.txt");
 41InBlock.gif                        if(txtFileInfo.Length>0)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 43InBlock.gif                            for(int i=0;i<txtFileInfo.Length;i++)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
 45InBlock.gif                                Console.WriteLine(ClearLine(txtFileInfo[i].FullName));
 46ExpandedSubBlockEnd.gif                            }

 47ExpandedSubBlockEnd.gif                        }

 48InBlock.gif                        else
 49ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 50ExpandedSubBlockStart.gifContractedSubBlock.gif                            Console.WriteLine(string.Format("指定目录\"dot.gif{0}\"并不存在要清除行号的文本文件.",tmpArg));
 51ExpandedSubBlockEnd.gif                        }

 52InBlock.gif
 53ExpandedSubBlockEnd.gif                        #endregion

 54ExpandedSubBlockEnd.gif                    }

 55InBlock.gif                    else
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 57ContractedSubBlock.gifExpandedSubBlockStart.gif                        Clear Line Numbers For The File#region Clear Line Numbers For The File
 58InBlock.gif                        //假定用户提供的参数为文件名,则先判断该文件是否存在,如果存在则清除该文件的行号
 59InBlock.gif                        if(File.Exists(tmpArg))
 60ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 61InBlock.gif                            Console.WriteLine(ClearLine(tmpArg));
 62ExpandedSubBlockEnd.gif                        }

 63InBlock.gif                        else
 64ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 65ExpandedSubBlockStart.gifContractedSubBlock.gif                            Console.WriteLine(string.Format("指定的文件或目录\"dot.gif{0}\"并不存在,请核对后重试.",tmpArg));
 66ExpandedSubBlockEnd.gif                        }

 67InBlock.gif
 68ExpandedSubBlockEnd.gif                        #endregion

 69ExpandedSubBlockEnd.gif                    }

 70ExpandedSubBlockEnd.gif                }

 71ExpandedSubBlockEnd.gif            }

 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif    
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 75InBlock.gif        /// 清除指定文件中的行号
 76InBlock.gif        /// </summary>
 77InBlock.gif        /// <param name="fileName">文件名,含路径</param>
 78ExpandedSubBlockEnd.gif        /// <returns>清除结果信息</returns>

 79InBlock.gif        public static string ClearLine(string fileName)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            string result;
 82InBlock.gif            FileInfo fi=new FileInfo(fileName);
 83InBlock.gif            string strExtension =fi.Extension;
 84InBlock.gif            try
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                using (StreamReader reader = new StreamReader(fileName, Encoding.Default, true))
 87ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 88InBlock.gif                    using (StreamWriter writer = new StreamWriter(fileName.Replace(strExtension,"_clear" + strExtension)))
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 90InBlock.gif                        char[] lineNum = "#0123456789".ToCharArray();
 91InBlock.gif                        string code = null;
 92InBlock.gif                        while ((code = reader.ReadLine()) != null)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 94InBlock.gif                            code = code.TrimStart();
 95InBlock.gif                            code = code.TrimStart(lineNum);
 96InBlock.gif                            writer.WriteLine(code);
 97ExpandedSubBlockEnd.gif                        }

 98ExpandedSubBlockEnd.gif                    }

 99ExpandedSubBlockEnd.gif                }

100InBlock.gif                result=string.Format("成功清除文件{0}的行号.",fileName);
101ExpandedSubBlockEnd.gif            }

102InBlock.gif            catch
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                result=string.Format("清除文件{0}的行号失败.",fileName);
105ExpandedSubBlockEnd.gif            }

106InBlock.gif
107InBlock.gif            return result;
108ExpandedSubBlockEnd.gif        }

109ExpandedSubBlockEnd.gif    }

110InBlock.gif
111ExpandedBlockEnd.gif}

112 None.gif

立即下载源码(for vs2003)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值