我该怎么处理互联网断线问题

时间:2015-12-11 16:56:21

标签: c#

我有一个程序可以获取目标目录中的所有文件,包括子目录中的文件。它为每个文件调用一个函数,然后创建另外两个文件。 问题是存在大量文件,我计划使用此程序定位的文件夹位于我需要互联网访问的服务器中。 我打算过几次运行这个过夜,但问题是如果互联网停机一秒钟,该程序可能会给出一些错误。如何确保如果互联网发生故障,程序将继续尝试在服务器中进行读/写,直到它恢复为止?

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      BindGrid();
    }

    if (!IsPostBack)
    {
      string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
      List<ListItem> files = new List<ListItem>();
      foreach (string filePath in filePaths)
      {
        var item = new ListItem(Path.GetFileNameWithoutExtension(filePath), filePath);
        if (!files.Contains(item))
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
      }
      DropDownList1.DataSource = files;
      DropDownList1.DataTextField = "";
      DropDownList1.DataValueField = "";
      DropDownList1.DataBind();
    }
  }
  protected void BindGrid()
  {
    string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in filePaths)
    {
      files.Add(new ListItem(Path.GetFileName(filePath), filePath));
    }
    GridView1.DataSource = files;
    GridView1.DataBind();
  }

如果有更好的解决方案,我想出了这个想法吗?

private void bw1_DoWork(object sender, DoWorkEventArgs e)
{
    String ar = (String) e.Argument;
    String outFolder = Path.Combine(ar, "outputFolder");
    Directory.CreateDirectory(outFolder);
    DirSearch(ar,outFolder);
}

private void DirSearch(String sDir,String outFolder)
{
    try
    {
        foreach (string f in Directory.GetFiles(sDir))
        {
            goRun(f,outFolder);
        }

        foreach (string d in Directory.GetDirectories(sDir))
        {
           if(!Path.GetFileName(d).Equals("outputFolder"))  DirSearch(d,outFolder);
        }
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.ToString());
    }
}

private void goRun(String path,String outFolder)
{
    String[] fileLines = File.ReadAllLines(path);
    String firstPath = Path.Combine(outFolder,"out1.txt")
    String secondPath = Path.Combine(outFolder,"out2.txt")
    //do something
    File.WriteAllLines(outputPath, outputOne);
    File.WriteAllLines(secondPath, outputTwo);
}

0 个答案:

没有答案