Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Csharp子字符串文本并将其添加到列表中_C#_String_File_Substring - Fatal编程技术网

C# Csharp子字符串文本并将其添加到列表中

C# Csharp子字符串文本并将其添加到列表中,c#,string,file,substring,C#,String,File,Substring,我有file.txt,比如: 编辑:我没有写,但我想这很重要-在file.txt中可能还有其他行 folder=c:\user;c:\test;c:\something; 我需要添加一个类似于一个列表项的路径(列表文件夹)。 因此,我的列表应该如下所示: Folders[0] = c:\user Folders[1] = c:\test 等等(不带文本“folder=”和“;”表示路径的结束) 文件可以包含更多的路径。 我是这样做的: using (FileStream fss

我有file.txt,比如: 编辑:我没有写,但我想这很重要-在file.txt中可能还有其他行

folder=c:\user;c:\test;c:\something;
我需要添加一个类似于一个列表项的路径(
列表文件夹
)。 因此,我的列表应该如下所示:

Folders[0] = c:\user
Folders[1] = c:\test
等等(不带文本“folder=”和“;”表示路径的结束)

文件可以包含更多的路径。 我是这样做的:

       using (FileStream fss = new FileStream(path, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(fss))
            {

                while (sr.EndOfStream == false)
                {
                    string line = sr.ReadLine();
                    if(line.StartsWith("folders"))
                    { 
                       int index = line.IndexOf("=");
                       int index1 = line.IndexOf(";");
                       string folder = line.Substring(index + 1, index1 - (index + 1));
                       Folders.Add(folder);

现在在列表文件夹中我有了第一条路径,但现在呢?我无法继续:(

您可以使用File.ReadAllText尝试以下代码

            string Filepath = "c:\abc.txt";
            string filecontent = File.ReadAllText(Filepath);
            string startingString = "=";
            var startIndex = filecontent.IndexOf(startingString);
            filecontent = filecontent.Substring(startIndex + 1, filecontent.Length - startIndex - 2);
            List<String> folders = filecontent.Split(';').ToList();
stringfilepath=“c:\abc.txt”;
字符串filecontent=File.ReadAllText(文件路径);
字符串开始字符串=“=”;
var startIndex=filecontent.IndexOf(startingString);
filecontent=filecontent.Substring(startIndex+1,filecontent.Length-startIndex-2);
列表文件夹=filecontent.Split(“;”).ToList();

您可以使用File.ReadAllText尝试以下代码

            string Filepath = "c:\abc.txt";
            string filecontent = File.ReadAllText(Filepath);
            string startingString = "=";
            var startIndex = filecontent.IndexOf(startingString);
            filecontent = filecontent.Substring(startIndex + 1, filecontent.Length - startIndex - 2);
            List<String> folders = filecontent.Split(';').ToList();
stringfilepath=“c:\abc.txt”;
字符串filecontent=File.ReadAllText(文件路径);
字符串开始字符串=“=”;
var startIndex=filecontent.IndexOf(startingString);
filecontent=filecontent.Substring(startIndex+1,filecontent.Length-startIndex-2);
列表文件夹=filecontent.Split(“;”).ToList();

以下是一个简单的示例:

    List<String> Folders = new List<string>();

    private void button1_Click(object sender, EventArgs e)
    {
        string path = @"C:\Users\mikes\Documents\SomeFile.txt";

        string folderTag = "folder=";
        using (FileStream fss = new FileStream(path, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(fss))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    if (line.StartsWith(folderTag))
                    {
                        line = line.Substring(folderTag.Length); // remove the folderTag from the beginning
                        Folders.AddRange(line.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                    }
                }
            }
        }

        foreach(string folder in Folders)
        {
            Console.WriteLine(folder);
        }
    }

下面是一个简单的例子:

    List<String> Folders = new List<string>();

    private void button1_Click(object sender, EventArgs e)
    {
        string path = @"C:\Users\mikes\Documents\SomeFile.txt";

        string folderTag = "folder=";
        using (FileStream fss = new FileStream(path, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(fss))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    if (line.StartsWith(folderTag))
                    {
                        line = line.Substring(folderTag.Length); // remove the folderTag from the beginning
                        Folders.AddRange(line.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                    }
                }
            }
        }

        foreach(string folder in Folders)
        {
            Console.WriteLine(folder);
        }
    }

为什么不呢?是什么阻止了你呢?你可以通过修剪
文件夹=
并按
进行拆分来简化这个过程。然后你可以使用
InsertRange
将整个拆分数组转储到列表中。完成。类似于line.Trim(char[“folders=“])为什么不呢?是什么阻止了你?你可以通过修剪
文件夹=
并按
进行拆分来简化这个过程。然后你可以使用
插入范围将整个拆分数组转储到列表中。
完成。类似于line.Trim(char[“folders=“])但是现在如何将每个路径添加到列表中?假设文件中只有一行。如果有多行以“folder=”开头怎么办?如果还有其他不应该处理的行呢?…事实上,还有其他行。对不起,我编辑了这个问题。@Kafus在你们的例子中只有一行。我不知道,其他行看起来怎么样!这就是我需要的:)我应该只替换变量
sr.ReadToEnd()
,一切正常。非常感谢,但是现在如何将每个路径添加到列表中呢?假设文件中只有一行。如果有多行以“folder=”开头怎么办?如果还有其他行不应该处理呢?…实际上,还有其他行。对不起,我编辑了这个问题。@Kafus在您的示例中只有一行。我不知道,其他人看起来怎么样!这就是我所需要的:)我应该只将
sr.ReadToEnd()
替换为我的变量
,一切正常。谢谢,是的,我的代码中有类似的东西。我的意思是,如果
,则为
。非常感谢。是的,我的代码中有类似的内容。我的意思是,如果,则为。谢谢。