小编Dee*_*Raj的帖子

使用C#检测文本文件的编码

我有一组markdown文件要传递给jekyll项目,需要找到它们的编码格式,即使用程序或API的带有BOM的UTF-8或没有BOM或ANSI的UTF-8。

如果我通过文件的位置,则必须列出文件,读取并作为结果产生编码。

是否有任何代码或API?

我已经尝试了有效方法中提到的用于流阅读器的sr.CurrentEncoding来找到任何文件的Encoding,但是结果随notepad ++结果的不同而不同。

还试图用https://github.com/errepi/ude(Mozilla的通用字符检测器),如建议https://social.msdn.microsoft.com/Forums/vstudio/en-US/862e3342-cc88-478f- bca2-e2de6f60d2fb / detect-encoding-of-file?forum = csharpgeneral,方法是在c#项目中实施ude.dll,但结果不如notepad ++那样有效,文件编码显示为utf-8,但从程序,结果是带有BOM的utf-8。

但是我应该从两种方法都得到相同的结果,那么问题出在哪里呢?

c# encoding utf-8

2
推荐指数
2
解决办法
3170
查看次数

我需要使用 C# 将具有特定字符串的行替换为另一个文件的内容

我有两个文件,File1.txt 和 File2.txt,我需要使用 C# 控制台应用程序将 file1 中的特定字符串替换为 file2 的内容。

文件1:

这是一个示例文件

..

代码块..

一些文字

文件结尾

文件2:

// 一些 C# 代码 //

我需要用文件 2 的内容替换字符串“code-block”。

编辑1:

我曾尝试将文件读取为数组和列表

var fileContent = File.ReadAllLines(file_path);
            List<string> allLinesText = File.ReadAllLines(file_path).ToList();
            string parentdirectory = Directory.GetParent(Path.GetDirectoryName(file_path)).FullName.ToString(); 

            foreach( var line in fileContent)
            {

                if ( line.Contains("{% code-block %}"))
                {
                    string code_path = line.Replace("{%", string.Empty).Replace("%}", string.Empty).Trim();
                    string code_fullpath = Path.Combine(parentdirectory, code_path);
                    if(File.Exists(code_fullpath))
                    {
                        var code_content = File.ReadAllLines(code_fullpath);
                       // int insert_code_at = 

                     // allLinesText.Insert( allLinesText.IndexOf(line)
                    }
                    
                }
Run Code Online (Sandbox Code Playgroud)

c# string list console-application

-1
推荐指数
1
解决办法
216
查看次数

标签 统计

c# ×2

console-application ×1

encoding ×1

list ×1

string ×1

utf-8 ×1