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

本文关键字:文件 代码 注释 读取 | 更新日期: 2023-09-27 18:33:50

我正在创建一个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() 也会读取注释的代码。我想跳过注释的代码,只想阅读未注释的代码。无论如何都可以这样做。谢谢。

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

如果使用 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);

您可以使用 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);
}