C#将请求得到的数据替换掉本地文件原有的数据

Program.cs

using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FaqJob
{
     
    class Program
    {
     
        private String id;
        private String thema;
        private int index;
        private string path = ConfigurationManager.AppSettings["Path"];
        private string url = ConfigurationManager.AppSettings["Url"];

        static void Main(string[] args)
        {
     
            var program = new Program();

            program.index = program.select(program.path);

            string new_result = program.getResult(program.url);        
            JArray jArray = JArray.Parse(new_result);
            foreach (var j in jArray)
            {
     
                JObject json = (JObject)j;
                program.id = json["id"].ToString();
                program.thema = json["thema"].ToString();

                program.EditFile(program.index, "
  • + program.id + "&artlang=zh\" target=\"_blank\">" + program.thema + "
  • "
    , program.path); program.index++; if (program.index == program.select(program.path) + 6) { break; } } } //获取接口数据 public string getResult(string url) { WebClient client = new WebClient(); //设置编码格式 client.Encoding = System.Text.Encoding.UTF8; //获取数据 string result = client.DownloadString(url); string new_result = result.Substring(22, result.Length - 23); return new_result; } //修改行数据 public void EditFile(int curLine, string newLineValue, string patch) { FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8")); string line = sr.ReadLine(); StringBuilder sb = new StringBuilder(); for (int i = 1; line != null; i++) { sb.Append(line + "\r\n"); if (i != curLine - 1) line = sr.ReadLine(); else { sr.ReadLine(); line = newLineValue; } } sr.Close(); fs.Close(); FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write); StreamWriter sw = new StreamWriter(fs1, Encoding.GetEncoding("utf-8")); sw.Write(sb.ToString()); sw.Close(); fs1.Close(); } //查询行数据 public int select(string patch) { int i = 0; StreamReader sr = new StreamReader(patch, Encoding.Default); string str = sr.ReadToEnd(); string[] strArr = str.Split(new string[] { "\r\n" }, StringSplitOptions.None); for ( i=0; i < strArr.Length; i++) { if (strArr[i].Contains("")) { break; } } sr.Dispose(); return i+2; } } }

    App.config

    
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        startup>
        <appSettings>
            
            <add key="Path" value="C:/Users/ben/Desktop/faq.html" />
            
            <add key="Url" value="https://faq.perfectclass.cn/main2.php?callback=success_jsonpCallback&_=1567734767662" />
        appSettings>
    configuration>
    

    你可能感兴趣的:(C#)