用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

批量去除行号程序 自动清除程序代码前的行号

2013-06-28 作者: 神马举报

[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();
            }
        }
    }
}

[代码运行效果截图]


批量去除行号程序 自动清除程序代码前的行号

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...