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

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

  1None.gifusing System;
  2None.gifusing System.IO;
  3None.gifusing System.Text;
  4None.gif
  5None.gifnamespace Ycweb
  6ExpandedBlockStart.gifContractedBlock.gifdot.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}

112None.gif

立即下载源码(for vs2003)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/305804.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

. NET5实战千万高并发项目,性能吊打JAVA,C#排名万年老五,有望逆袭!

“秒杀活动”“抢红包”“微博热搜”“12306抢票”“共享单车拉新”等都是高并发的典型业务场景&#xff0c;那么如何解决这些业务场景背后的难点问题呢&#xff1f;秒杀系统中&#xff0c;QPS达到10万/s时&#xff0c;如何定位并解决业务瓶颈&#xff1f;明星婚恋话题不断引爆…

java不朽神迹,不朽的神迹 Eternal Legacy HD v1.0.8

游戏简介不朽的神迹是一个拥有全3D实时渲染的史诗战斗场面、360度自由调整的视角及丰富的动作特效的游戏。玩家将在游戏中探索壮丽的大陆&#xff0c;体验张力十足的战斗系统。游戏提供了多至3名角色同时参战&#xff0c;可从队伍成员中选择出战的队友&#xff0c;并且定义他们…

ABP vNext 审计日志获取真实客户端IP

背景在使用ABP vNext时&#xff0c;当需要记录审计日志时&#xff0c;我们按照https://docs.abp.io/zh-Hans/abp/latest/Audit-Logging配置即可开箱即用&#xff0c;然而在实际生产环境中&#xff0c;某些配置并不可取&#xff0c;比如今天的主角——客户端IP&#xff0c;记录用…

郭昶

郭 昶左直拳饰演《外来媳妇本地郎》中康家老二康祁宗的演员郭昶6月14日去世了&#xff0c;胃癌&#xff0c;享年50岁。这个消息真令人难以置信&#xff0c;不胜嘘唏。 《外来媳妇本地郎》在广东这边很受欢迎&#xff0c;每集结尾那带有浓厚岭南特色的粤曲小调在胡同小巷时有…

php 常用rpc框架,php的轻量级rpc框架yar

php的轻量级rpc框架yar目的&#xff1a;类方法的远程调用,也就是一个rpc请求。RPC本质上也是一个网络请求&#xff0c;既然是请求&#xff0c;对于效率来说&#xff0c;就需要考虑了。yar是基于http来做的。使用场景&#xff1a;多个项目共享model总的来说这种调用代价挺好的&a…

ABP vNext IOC替换原有Service实现

即 .NET IOC替换原有Service实现背景在使用ABP vNext时&#xff0c;该框架为我们实现了非常多的默认行为&#xff0c;以便开箱即用&#xff0c;但在实际使用中&#xff0c;我们总是需要根据自己的需求定制自己的服务&#xff0c;在.Net框架中&#xff0c;便提供了Service.Repla…

aqs java 简书,Java AQS源码解读

1、先聊点别的说实话&#xff0c;关于AQS的设计理念、实现、使用&#xff0c;我有打算写过一篇技术文章&#xff0c;但是在写完初稿后&#xff0c;发现掌握的还是模模糊糊的&#xff0c;模棱两可。痛定思痛&#xff0c;脚踏实地重新再来一遍。这次以 Java 8源码为基础进行解读。…

仓储模式到底是不是反模式?

【导读】仓储模式我们已耳熟能详&#xff0c;但当我们将其进行应用时&#xff0c;真的是那么得心应手吗&#xff1f;确定是解放了生产力吗&#xff1f;这到底是怎样的一个存在&#xff0c;确定不是反模式&#xff1f;一篇详文我们探讨仓储模式&#xff0c;这里仅我个人的思考&a…

网络工程师必须懂的十五大专业术语!

1、什么时候使用多路由协议&#xff1f; 当两种不同的路由协议要交换路由信息时&#xff0c;就要用到多路由协议。当然&#xff0c;路由再分配也可以交换路由信息。下列情况不必使用多路由协议&#xff1a; 从老版本的内部网关协议&#xff08; Interior Gateway Protocol&…

dnSpy反编译、部署调试神器

一、概要在工作当中&#xff0c;当程序部署了之后就算打了日志遇到极个别的特殊异常没有在程序日志中体现出来或者没有详细的报错原因会让开发者非常头疼&#xff0c;不得不盲猜bug到底出在哪里。这里分享一下工作上经常会用到的工具&#xff0c;这款工具可以反编译并运行调试已…

java中内边距跟外边距,padding和margin——内边距和外边距

一、padding——内边距(内填充)1.1、padding 简写属性在一个声明中设置所有填充属性。该属性可以有1到4个值。div.outer{width: 400px;height: 400px;border: 2px solid #000;}div.inner{width: 200px;height: 200px;background-color:red ;padding: 50px;}运行效果图&#xff…

AJAX将成为移动Web2.0时代首选开发平台

一、 引言  最近,Opera宣布通过他们的浏览器把AJAX技术应用于移动设备开发中。考虑到Opera浏览器在目前浏览器市场(特别是在移动浏览器市场)的流行性,我们可以预计这一宣布对于整个浏览器市场必然会产生重要影响。从加入到移动服务开发市场几年的经验来看&#xff0c;我相信现…

matlab仿真习题,(MATlab仿真部分习题答案.doc

(MATlab仿真部分习题答案[4.1]控制系统结构如图4.1所示利用MATLAB对以上单位负反馈控制系统建立传递函数&#xff1b;将第一问中求得的传递函数模型转化为零极点增益形式和状态空间形式。解:(1)num[2 2];den[1 2 1];[num1,den1]cloop(num,den);systf(num1,den1)程序运行结果如下…

使用 ML.NET 实现峰值检测来排查异常

机器学习中一类问题称为峰值检测&#xff0c;它旨在识别与大部分时序中明显不同但临时突发的数据值。及时检测到这些可疑的个体、事件或观察值很重要&#xff0c;这样才能尽量减少其产生。异常情况检测是检测时序数据离群值的过程&#xff0c;在给定的输入时序上指向“怪异”或…

如何使用Tasklist命令

Tasklist命令用来显示运行在本地或远程计算机上的所有进程&#xff0c;带有多个执行参数。使用格式Tasklist [/S system [/U username [/P [password]]]] [/M [module] | /SVC | /V] [/FI filter] [/FO format] [/NH]参数含义/S system 指定连接到的远程系统。/U [domain]user…

PHP防QQ列表右划,react native 实现类似QQ的侧滑列表效果

如果列表行数据需要更多的操作&#xff0c;使用侧滑菜单是移动端比较常见的方式&#xff0c;也符合用户的操作习惯&#xff0c;对app的接受度自然会相对提高点。最近得空就把原来的react-native项目升级了侧滑操作&#xff0c;轻轻松松支持android和ios双平台&#xff0c;效果如…

OpenTelemetry - 云原生下可观测性的新标准

CNCF 简介CNCF&#xff08;Cloud Native Computing Foundation&#xff09;&#xff0c;中文为“云原生计算基金会”&#xff0c;CNCF是Linux基金会旗下的基金会&#xff0c;可以理解为一个非盈利组织。当年谷歌内部一直用于编排容器的Borg项目开源了&#xff0c;为了该项目更好…

毕业设计——第三章 开发方法及系统实现(5)

国内私募机构九鼎控股打造APP&#xff0c;来就送 20元现金领取地址&#xff1a;http://jdb.jiudingcapital.com/phone.html内部邀请码&#xff1a;C8E245J &#xff08;不写邀请码&#xff0c;没有现金送&#xff09;国内私募机构九鼎控股打造&#xff0c;九鼎投资是在全国股份…

说说 RabbiMQ 的应答模式

RabbiMQ 我们都很熟悉了&#xff0c;是很常用的一个开源消息队列。搞懂 RabbiMQ 的应答模式对我们排查错误很有帮助&#xff0c;也能避免一些坑。本文说说 RabbiMQ 的应答模式。生产者发出一条消息给 RabbiMQ &#xff0c;RabbiMQ 将消息推送给消费者&#xff0c;消费者处理完消…

php 输出json utf8,php json_encode utf-8中文问题

以前碰到最多的是json_encode是gbk 编码时出现乱码&#xff0c;今天发现uft8也会出现中文乱码了&#xff0c;下面我们一起看问题如何解决吧。utf-8字符json_encode为变成转成utf16编码&#xff0c;也就是介个样子&#xff1a; 代码如下复制代码$ ./php/bin/php -r echo json_en…