如何读取整个文件但跳过c#中的注释代码

时间:2016-02-09 17:20:40

标签: c# asp.net winforms c#-4.0

我正在创建一个Windows应用程序,它将读取目录中的所有文件,这是代码。

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()也会读取注释的代码。我想跳过注释代码,只想阅读未注释的代码。无论如何要做到这一点。 感谢。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您可以使用Regex.Replace方法。像这样的样本

public static string ReadAllLinesWithoutComment()
{
    string path=@"";
    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);
}