去除代码行号的一个小程序
今天游览博客园,看别人的代码,觉得写得不错,想copy下来,无奈代码却有行号,我一向是先把代码copy下来然后一行一行的去除的,不过想想以后如果总是这么做的话也太麻烦了,于是终于决定写一个工具软件来做这件事情,共享给大家,希望能有所帮助。程序基于Microsoft Visual Studio 2005,主要代码如下: 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
using System.IO;
10
namespace ClearCodeLineNum
11
{
12
public partial class Form1 : Form
13
{
14
public Form1()
15
{
16
InitializeComponent();
17
18
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
19
openFileDialog1.FileName = null;
20
}
21
22
private void btnOpenFile_Click(object sender, EventArgs e)
23
{
24
if (openFileDialog1.ShowDialog() == DialogResult.OK)
25
{
26
txtFilePath.Text = openFileDialog1.FileName;
27
btnClearCode.Focus();
28
}
29
}
30
31
private void btnClearCode_Click(object sender, EventArgs e)
32
{
33
if (File.Exists(txtFilePath.Text))
34
{
35
using (StreamReader reader = new StreamReader(txtFilePath.Text, Encoding.Default, true))
36
{
37
int index = txtFilePath.Text.LastIndexOf('.');
38
39
///strExtension is the file extension, we need to maintain the
40
///cleared file's extension
41
string strExtension = txtFilePath.Text.Substring(index);
42
using (StreamWriter writer = new StreamWriter(txtFilePath.Text + ".clear" + strExtension))
43
{
44
///lineNum will be the array of numbers that need removing
45
///from every line code.
46
char[] lineNum = "0123456789".ToCharArray();
47
string code = null;
48
while ((code = reader.ReadLine()) != null)
49
{
50
///remove the spaces at the front of the line,otherwise
51
///it will stop the operation of the next line code.
52
code = code.TrimStart();
53
code = code.TrimStart(lineNum);
54
writer.WriteLine(code);
55
}
56
}
57
}
58
MessageBox.Show("成功去除行号", "清风竹林", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
59
}
60
btnOpenFile.Focus();
61
}
62
63
private void btnCopyPath_Click(object sender, EventArgs e)
64
{
65
if (File.Exists(txtFilePath.Text))
66
{
67
FileInfo fileInfo = new FileInfo(txtFilePath.Text);
68
System.Diagnostics.Process.Start(fileInfo.DirectoryName);
69
btnOpenFile.Focus();
70
}
71
}
72
}
73
}
呵呵,现在好了,如上面的代码,只要copy下来保存成文本,用本程序就可以很容易的去除了。
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
using System.IO;10
namespace ClearCodeLineNum11
{12
public partial class Form1 : Form13
{14
public Form1()15
{16
InitializeComponent();17

18
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);19
openFileDialog1.FileName = null;20
}21

22
private void btnOpenFile_Click(object sender, EventArgs e)23
{24
if (openFileDialog1.ShowDialog() == DialogResult.OK)25
{26
txtFilePath.Text = openFileDialog1.FileName;27
btnClearCode.Focus();28
}29
}30

31
private void btnClearCode_Click(object sender, EventArgs e)32
{33
if (File.Exists(txtFilePath.Text))34
{35
using (StreamReader reader = new StreamReader(txtFilePath.Text, Encoding.Default, true))36
{37
int index = txtFilePath.Text.LastIndexOf('.');38

39
///strExtension is the file extension, we need to maintain the 40
///cleared file's extension41
string strExtension = txtFilePath.Text.Substring(index);42
using (StreamWriter writer = new StreamWriter(txtFilePath.Text + ".clear" + strExtension))43
{44
///lineNum will be the array of numbers that need removing45
///from every line code.46
char[] lineNum = "0123456789".ToCharArray();47
string code = null;48
while ((code = reader.ReadLine()) != null)49
{50
///remove the spaces at the front of the line,otherwise 51
///it will stop the operation of the next line code.52
code = code.TrimStart();53
code = code.TrimStart(lineNum);54
writer.WriteLine(code);55
}56
}57
}58
MessageBox.Show("成功去除行号", "清风竹林", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);59
}60
btnOpenFile.Focus();61
}62

63
private void btnCopyPath_Click(object sender, EventArgs e)64
{65
if (File.Exists(txtFilePath.Text))66
{67
FileInfo fileInfo = new FileInfo(txtFilePath.Text);68
System.Diagnostics.Process.Start(fileInfo.DirectoryName);69
btnOpenFile.Focus();70
}71
}72
}73
}源代码下载: ClearCodeLineNum.rar



浙公网安备 33010602011771号