根据该块上的标记读取一段文本 [英] Read a block of text basing on a tag on that block

查看:86
本文介绍了根据该块上的标记读取一段文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多文本文件,每个文件只包含大约5行。这些文件包含我在我的方法中需要的某些元素,但由于我有很多方法,我现在在一个文件夹中有超过10个文本文件,虽然目前它们没有问题,我认为文本太多了文件将来会出现问题。所以我想知道是否有办法让我在基于标签的文本文件中读取块。比方说我会在文本文件中有这个内容



I have a multiple texts files that contains only about 5 lines per file. These files contains certain elements that I need on my methods, but since I have a lot of methods I am now having more than 10 texts files in a folder, though having them is non-problematic at the moment, I think having too many text files would pose a problem in the future. So I was wondering if there is a way for me to read a block in a text file basing on tag. say for example I would have this content in a textfile

[object1]
assdfg
asdfghjhgyffhgj
sdghteshjpjkpogkwpotkopg
asdfsfhthhsfgfgsg

[object2]
sadfgjrtsg
gryrhdjdhdhd
retteryhdhdf
sd
asdar





我想要实现的是在我的C#项目中调用object1的方法,它将返回以下所有行的数据字符串标签[object1],同样适用于object2。如果返回的字符串只变为1行就可以了,但是保留行号会很棒:)



如果已有样品或答案请参考链接。提前致谢。 :)



what I'm trying to achieve is to have a method to call in my C# project to read "object1" and it will return data string of all lines below the tag [object1],same goes for object2. It's even ok if the returned string will become 1 line only, but it would be wonderful to retain the line numbers :)

Should there be already existing samples or answers please refer a link. Thanks in advance. :)

推荐答案

如果我理解你,你想要一个读/写.ini文件或类似方法的方法。试试这个使用C#的INI文件处理类 [ ^ ]





编辑:



好​​的,试试看整整一节:



首先,添加此dll导入:

So you want, if i understand you, a method to read/write .ini files or something similar. Try this An INI file handling class using C#[^]




Ok,try this to read a whole section:

First,add this dll import:
[DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);





然后,添加此方法:



and then,add this method:

public string[] IniReadSection(string Section)
        {
            
            byte[] buffer = new byte[2048];
            GetPrivateProfileSection(Section, buffer, 2048, this.path);
            String[] tmp = Encoding.ASCII.GetString(buffer).Trim('\0').Split('\0');
            
            return tmp;

        }





这将返回一个字符串数组,其中包含该部分中的所有键



That will return a string array with all the keys in that section


这篇关于根据该块上的标记读取一段文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆