2016-02-09 21 views
0

我正在創建一個Windows應用程序,將讀取目錄中的所有文件,這裏是代碼。如何閱讀整個文件,但跳過註釋的代碼在C#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace LathropFileScan 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnprint_Click(object sender, EventArgs e) 
     { 
      TextWriter text = new StreamWriter("FileScan.csv"); 
      List<string> files = new List<string>(); 

      foreach (string folders in Directory.GetDirectories(@"C:\Users\Desktop\Application","*",SearchOption.AllDirectories)) 
      { 
       foreach (string file in Directory.GetFiles(folders)) 
       { 
        int counter = 0; 

        foreach (string innerfile in Directory.GetFiles(folders)) 
        { 

          string content = File.ReadAllText(innerfile); 

         if (content.Contains(Path.GetFileName(file))) 
          counter++; 

        } 

        if (counter == 0) 
        { 

          files.Add(file.Substring(34)); 

        } 
       } 
      } 
      foreach (string print in files) 
      { 
       text.Write(print + Environment.NewLine); 
      } 

      text.Close(); 
     } 
    } 
} 

通過使用此代碼,我可以成功讀取目錄的所有文件,但File.ReadAllText()也讀取註釋的代碼。我想跳過評論的代碼,只想讀取未註釋的代碼。無論如何要這樣做。 謝謝。

+0

樣品,你不得不使用某種形式的正則表達式來查找註釋模式//和/ ** /等,並排除那些 –

+3

其中「註釋的代碼」?您正在閱讀的文件中是否包含這些內容?也許它是html,也許它是c#或java,也許它是基本的。他們都有不同的註釋符號。 – spender

+1

當然,只需檢查每行「//」,如果找到則忽略;和「/ *」,並繼續忽略,直到找到結束註釋(「* /」)。 –

回答

3

如果使用ReadAllLines而不是ReadAllText,則波紋管代碼可以正常工作。

var newString = ""; 
     var lines = File.ReadAllLines("").ToList(); 
     foreach (var line in lines) 
     { 
      if (line.Contains("//") && line.EndsWith("\n")) 
      { 
       var startIndex = line.IndexOf("//", StringComparison.Ordinal); 
       var endIndex = line.LastIndexOf("\n", StringComparison.Ordinal); 
       var length = (endIndex + 1) - startIndex; 
       var subString = line.Substring(startIndex, length); 
       var temp = line.Replace(subString, ""); 
       newString += temp; 
       //Console.WriteLine(line); 
       continue; 
      } 
      newString += line; 
     } 
     //Console.WriteLine(newString); 
0

您可以使用Regex.Replace方法。這樣

public static string ReadAllLinesWithoutComment() 
    { 
     string [email protected]""; 
     string result = string.Empty; 
     string[] tempArry = File.ReadAllLines(path); 

     for (int i = 0; i < tempArry.Length; i++) 
      if (tempArry[i].Contains(@"//")) 
       tempArry[i] = tempArry[i].Substring(0, tempArry[i].IndexOf(@"//")); 

     result = string.Join(Environment.NewLine, tempArry); 

     string pattern = "(/[*]).*[*]/"; // Remove multi line comment (/* */) 
     string replacement = ""; 
     Regex rgx = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); 

     return rgx.Replace(result, replacement); 
    }