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

{
7
/**//// <summary>
8
/// Summary description for Class1.
9
/// </summary>
10
class CLN
11
{
12
/**//// <summary>
13
/// The main entry point for the application.
14
/// </summary>
15
[STAThread]
16
static void Main(string[] args)
17
{
18
//
19
// TODO: Add code to start application here
20
//
21
if(args.Length<1)
22
{
23
Console.WriteLine("用法:\n\r\t CLN YourFile.TXT|YourDirectory");
24
}
25
else
26
{
27
string tmpArg=args[0];
28
29
if(tmpArg.StartsWith("/") || tmpArg.StartsWith("?"))
30
{
31
Console.WriteLine("用法:\n\r\t CLN YourFile.TXT|YourDirectory");
32
}
33
else
34
{
35
//假定用户提供的参数为目录,则先判断目录是否存在,如果存在则遍历该目录下的所有文本文件并清除行号
36
if(System.IO.Directory.Exists(tmpArg))
37
{
38
Clear Line Numbers For Files In The Directory#region Clear Line Numbers For Files In The Directory
39
DirectoryInfo di=new DirectoryInfo(tmpArg);
40
FileInfo[] txtFileInfo = di.GetFiles("*.txt");
41
if(txtFileInfo.Length>0)
42
{
43
for(int i=0;i<txtFileInfo.Length;i++)
44
{
45
Console.WriteLine(ClearLine(txtFileInfo[i].FullName));
46
}
47
}
48
else
49
{
50
Console.WriteLine(string.Format("指定目录\"
{0}\"并不存在要清除行号的文本文件.",tmpArg));
51
}
52
53
#endregion
54
}
55
else
56
{
57
Clear Line Numbers For The File#region Clear Line Numbers For The File
58
//假定用户提供的参数为文件名,则先判断该文件是否存在,如果存在则清除该文件的行号
59
if(File.Exists(tmpArg))
60
{
61
Console.WriteLine(ClearLine(tmpArg));
62
}
63
else
64
{
65
Console.WriteLine(string.Format("指定的文件或目录\"
{0}\"并不存在,请核对后重试.",tmpArg));
66
}
67
68
#endregion
69
}
70
}
71
}
72
}
73
74
/**//// <summary>
75
/// 清除指定文件中的行号
76
/// </summary>
77
/// <param name="fileName">文件名,含路径</param>
78
/// <returns>清除结果信息</returns>
79
public static string ClearLine(string fileName)
80
{
81
string result;
82
FileInfo fi=new FileInfo(fileName);
83
string strExtension =fi.Extension;
84
try
85
{
86
using (StreamReader reader = new StreamReader(fileName, Encoding.Default, true))
87
{
88
using (StreamWriter writer = new StreamWriter(fileName.Replace(strExtension,"_clear" + strExtension)))
89
{
90
char[] lineNum = "#0123456789".ToCharArray();
91
string code = null;
92
while ((code = reader.ReadLine()) != null)
93
{
94
code = code.TrimStart();
95
code = code.TrimStart(lineNum);
96
writer.WriteLine(code);
97
}
98
}
99
}
100
result=string.Format("成功清除文件{0}的行号.",fileName);
101
}
102
catch
103
{
104
result=string.Format("清除文件{0}的行号失败.",fileName);
105
}
106
107
return result;
108
}
109
}
110
111
}
112

2

3

4

5

6



7


8

9

10

11



12


13

14

15

16

17



18

19

20

21

22



23

24

25

26



27

28

29

30



31

32

33

34



35

36

37



38


39

40

41

42



43

44



45

46

47

48

49



50



51

52

53

54

55

56



57


58

59

60



61

62

63

64



65



66

67

68

69

70

71

72

73

74


75

76

77

78

79

80



81

82

83

84

85



86

87



88

89



90

91

92

93



94

95

96

97

98

99

100

101

102

103



104

105

106

107

108

109

110

111

112

立即下载源码(for vs2003)