[c#]代码库
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ClearCodeLineNum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
openFileDialog1.FileName = null;
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = openFileDialog1.FileName;
btnClearCode.Focus();
}
}
private void btnClearCode_Click(object sender, EventArgs e)
{
if (File.Exists(txtFilePath.Text))
{
using (StreamReader reader = new StreamReader(txtFilePath.Text, Encoding.Default, true))
{
int index = txtFilePath.Text.LastIndexOf('.');
///strExtension is the file extension, we need to maintain the
///cleared file's extension
string strExtension = txtFilePath.Text.Substring(index);
using (StreamWriter writer = new StreamWriter(txtFilePath.Text + ".clear" + strExtension))
{
///lineNum will be the array of numbers that need removing
///from every line code.
char[] lineNum = "0123456789".ToCharArray();
string code = null;
while ((code = reader.ReadLine()) != null)
{
///remove the spaces at the front of the line,otherwise
///it will stop the operation of the next line code.
code = code.TrimStart();
code = code.TrimStart(lineNum);
writer.WriteLine(code);
}
}
}
MessageBox.Show("成功去除行号", "清风竹林", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
btnOpenFile.Focus();
}
private void btnCopyPath_Click(object sender, EventArgs e)
{
if (File.Exists(txtFilePath.Text))
{
FileInfo fileInfo = new FileInfo(txtFilePath.Text);
System.Diagnostics.Process.Start(fileInfo.DirectoryName);
btnOpenFile.Focus();
}
}
}
}
[代码运行效果截图]
[源代码打包下载]
by: 发表于:2018-01-29 11:00:05 顶(0) | 踩(0) 回复
??
回复评论