2016-10-13 5 views
-1

ちょっとの合計行を表示するWindowsアプリケーションを作成したい空白行とコメント行。私は合計行を計算することができます。空白行とコメント行のロジックを手助けすることができますか?C#WindowsアプリケーションLOC_Counter

私は、すなわち.htmlを、.cssの、.csファイル任意のファイルのために、コードの行数をカウントしたい、など

また、私は結果はエクセルファイルにエクスポートしたい可能であれば!


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


    } 

}


私は、空白行の数を表示する必要がある複数のテキストボックスをしたい行をコメントし、最終的に合計数(すなわちなしを差し引く。空白行のとコメント行数の合計からの行数)

可能な場合は、1つのボタンオールまたはすべてのボタンをクリックします。 enter image description here

+0

あなたは何を試しましたか、正確にどこにいるのですか?あなたが投稿したコードは全体の行数をカウントします... – bassfader

+0

はい、私は空白行とコメント行を数える必要があります。 ------------------------------------------------- ------------------------------------------ if(str!= null && str.Length> 0 &&(str.Length> 2 && str.Substring(0、2)!= "//")&&(str.Length> 3 && str.Substring(0、4)!= "<! - - ")) 私はこのロジックを使用していますが、button_clickを使ってテキストボックスに表示する方法については助けが必要です –

答えて

0
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(); 
     } 
    } 
} 
関連する問題