c# Windows应用程序

本文关键字:应用程序 Windows | 更新日期: 2023-09-27 18:12:54

嘿,我想创建一个windows应用程序,应该显示总行空白行和注释行。我能计算总行,谁能帮我解释一下空白行和注释行的逻辑?

我想计算代码行数,对于任何文件,即。html,。css,。cs等。

如果可能的话,我还希望将结果导出到Excel文件!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Line_Counter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        { 
         OpenFileDialog ofd = new OpenFileDialog();
            textbox1.Clear();
            textbox2.Clear();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string strFilename = ofd.FileName;
                textbox1.Text = Path.GetFileName(strFilename);
                StreamReader sr = File.OpenText(strFilename);
                int nLineCount = 0;
                while (sr.ReadLine() != null)
                {
                    nLineCount++;    
                }

                textbox1.Text = nLineCount.ToString("0,0");
                sr.Close();
            }
        }
        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

}


我想要更多的文本框,应该显示空行计数,注释行,最后的总计数(即减去no。空白行和注释行从总行数)

如果可能,单个button_click为每个或所有在一个。此处输入图像描述

c# Windows应用程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace LOC_Counter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void getFileList(string directory)
        {
            string[] dirs = Directory.GetDirectories(directory);
            foreach (string dir in dirs)
            {
                getFileList(dir);
            }
            string[] files = Directory.GetFiles(directory);
            foreach (string file in files)
            {
                StreamReader sr = File.OpenText(file);
                int nLineCount = 0;
                string str = string.Empty;
                while (!sr.EndOfStream)
                {
                    str = sr.ReadLine();
                    if (str != null && str.Length > 0 && (str.Length > 2 && str.Substring(0, 2) != "//") && (str.Length > 3 && str.Substring(0, 4) != "<!--"))
                    nLineCount++;
                }
                lstbxResult.Items.Add(file + "              Line count - " + nLineCount);
            }

        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            if (FBD.ShowDialog() == DialogResult.OK)
            {
                lstbxResult.Items.Clear();
                getFileList(FBD.SelectedPath);
            }
        }
        public static void ExportToExcel(ListBox lst, string excel_file)
        {
            int cols;
            //open file
            StreamWriter wr = new StreamWriter(excel_file);
            //write rows to excel file
            for (int i = 0; i < (lst.Items.Count - 1); i++)
            {
                wr.Write(lst.Items[i].ToString() + "'t");
                wr.WriteLine();
            }
            //close file
            wr.Close();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}