C# 如何使用c设置txt文件中每行的preftix和delete after字符#

C# 如何使用c设置txt文件中每行的preftix和delete after字符#,c#,C#,您好,我从Web API中获取了一些字符串 它给我一些字符串记录,我想做这个操作 我想让它一行一行地像原版一样 我想删除每个行中逗号后的所有内容 我想在示例的每一行之前设置一个前缀: 示例:Google.com至>>这是Dns:Google.com 这是我的密码。我应该编辑什么,应该添加什么 string filePath = "D:\\google.txt"; WebClient wc = new WebClient(); string htm

您好,我从Web API中获取了一些字符串

它给我一些字符串记录,我想做这个操作

  • 我想让它一行一行地像原版一样

  • 我想删除每个行中逗号后的所有内容

  • 我想在示例的每一行之前设置一个前缀:

  • 示例:
    Google.com至>>这是Dns:Google.com

    这是我的密码。我应该编辑什么,应该添加什么

            string filePath = "D:\\google.txt";
            WebClient wc = new WebClient();
            string html = wc.DownloadString("https://api.hackertarget.com/hostsearch/?q=Google.com");
            File.CreateText(filePath).Close();
            string number = html.Split(',')[0].Trim();
            File.WriteAllText(filePath, number);
            MessageBox.Show("Finish");
    

    你真的很接近。检查下面的解决方案

    var filePath = @"C:\Users\Mirro\Documents\Visual Studio 2010\Projects\Assessment2\Assessment2\act\actors.txt";
    
    WebClient client = new WebClient();
    string html = client.DownloadString("https://api.hackertarget.com/hostsearch/?q=google.com");
    string[] lines = html.Split(
        new[] { "\r\n", "\r", "\n" },
        StringSplitOptions.None
    );
    
    var res = lines.Select(x => (x.Split(',')[0]).Trim()).ToList();
    
    //res.Dump();
    System.IO.File.WriteAllLines(filePath, lines);
    

    你好 多亏了

    我也从另一个角度找到了我的解决方案

            string path = @"D:\Google.txt";
            var url = "https://api.hackertarget.com/hostsearch/?q=google.com";
            var client = new WebClient();
            //StreamWriter myhost = new StreamWriter(path);
            using (var stream = client.OpenRead(url))
            using (var reader = new StreamReader(stream))
            {
                string line;
                string realString;
                using (StreamWriter myhost = new StreamWriter(path))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        realString = "This is the dns: " + line.Split(',')[0].Trim();
    
                        myhost.WriteLine(realString);
                    }
                }
            }
            MessageBox.Show("Finish");
    

    请显示更多的代码,我们不知道什么是wc,即使我们可以猜到它是一个下载html内容的网络客户端,我们也不需要。另外,您没有提到将任何内容保存到文件中,如果您不想将输出保存到文件中,请在内存中工作,直到获得所需内容,然后使用一个将创建/打开/写入内容并关闭文件的
    writealText()
    写入文件。
            string path = @"D:\Google.txt";
            var url = "https://api.hackertarget.com/hostsearch/?q=google.com";
            var client = new WebClient();
            //StreamWriter myhost = new StreamWriter(path);
            using (var stream = client.OpenRead(url))
            using (var reader = new StreamReader(stream))
            {
                string line;
                string realString;
                using (StreamWriter myhost = new StreamWriter(path))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        realString = "This is the dns: " + line.Split(',')[0].Trim();
    
                        myhost.WriteLine(realString);
                    }
                }
            }
            MessageBox.Show("Finish");