2014-03-18 32 views
0

我想根據某些數據用文件的現有行替換文本的行。我開發了一些代碼塊,但沒有奏效。 我的文本文件是這樣的: -用C#中相同文本文件的另一行代替行

g_start-fd,g_start-cnst,g_start-eq,mv-mv_size,mv-mv_alloy,mv-mv_argmt,mv-mv_ps,xfrmr-kva,g_end-line_t,g_end-str_num,g_end-cmt,g_end-str_coord-Latitude,g_end-str_coord-Longitude 
28F1Y,oh,mv oh,120,al,oh_3,P45R24,,i,P45R25,,9.53725695,-0.86668464 
28F1Y,oh,mv oh,120,al,oh_3,P45R25,,i,P45R42,,9.5468355,-0.85948875 
28F1Y,oh,mv oh,120,al,oh_3,P45R42,,i,P45R49,,9.55073989,-0.85625858 
28F1Y,oh,mv oh,120,al,oh_3,P45R49,,a,P45R25,,, 
28F1Y,oh,mv oh,120,al,oh_3,P45R54,,i,P45R55,,9.5544981,-0.85359626 
28F1Y,oh,mv xfrmr,120,al,oh_3,P45R55,5000,e,P45R56,Substation,9.5549907,-0.85303108 
28F1Y,ug,mv,185,al,xlpe_3,P45R56,,e,P45R55,,, 
28F1Y,ug,mv,185,al,xlpe_3,P45R57,,s,P45R58,Take off from ring main,9.55387622,-0.8538622 
28F1Y,oh,mv oh,120,al,oh_3,P45R58,,a,P45R73,,9.54513187,-0.86060037 
28F1Y,oh,mv oh,120,al,oh_3,P45R73,,a,P45R77,,9.5417936,-0.86098952 
28F1Y,oh,mv oh,120,al,oh_3,P45R77,,a,P45R80,,9.54144045,-0.85857346 
28F1Y,oh,mv oh,120,al,oh_3,P45R80,,a,P45R86,,9.53675765,-0.85935176 
28F1Y,oh,mv,120,al,oh_3,P45R86,,e,P45R80,,, 

我的應用程序停止工作,當我運行這段代碼:

string fileName1 = "D:\\WriteTextWork\\Line1.txt"; ; 

OpenFileDialog pfdg = new OpenFileDialog(); 
if (pfdg.ShowDialog() == DialogResult.OK) 
{ 
    fileName1 = pfdg.FileName; 
} 

if (File.Exists(fileName1)) 
{ 
    StreamReader SR = new StreamReader(fileName1); 

    string Data = null; 
    int count = 0; 

    while ((Data = SR.ReadLine()) != null) 
    { 
     count++; 
     if (count > 1) 
     { 
      string CopyText = ""; 
      String[] SplitData = Data.Split(','); 
      if (SplitData[9] != null && SplitData[11] != null) 
      { 
       CopyText = Data; 
       string data1 = SR.ReadLine(); 
       //MessageBox.Show(CopyText); 
      } 
      using (StreamReader SR1 = new StreamReader(fileName1)) 
      { 
       //var SW = new StreamWriter(resultString1); 
       string line; 
       while ((line = SR1.ReadLine()) != null) 
       { 


        //String TrimData2 = line.Trim(); 
        String[] SplitText = line.Split(','); 
        if (SplitText[9] == SplitData[9] && SplitText[11] == null) 
        { 
         using (StreamWriter SW = new StreamWriter(resultString1)) 
         { 
          SW.WriteLine(CopyText); 
          MessageBox.Show(CopyText); 
          SW.Close(); 
         } 

        } 
       } 
       SR1.Close(); 
      } 
     } 
    } 
} 
+0

你可以發佈錯誤以及哪部分代碼會拋出錯誤嗎? – failedprogramming

+3

「停止工作」不是對發生什麼的技術描述。如果你需要幫助,你需要學會像程序員一樣思考,做一些技術觀察,然後在你的問題中報告這些細節。 –

+2

您正在創建嵌套的讀者和作家。想一想,這有道理嗎?考慮1個閱讀器和1個相同級別的作家以及與你的邏輯相匹配的複製行。 –

回答

0

我做了一個事件(btn_proceed_Click)來進行該方法,您可以將他(方法)放在任何你想要的地方。下面是完整的代碼,只需用你的更換:

private void btn_proceed_Click(object sender, EventArgs e) 
    { 
     // This dictionary contains indices of rows we need to replace. 
     Dictionary<int, int> replaceable = new Dictionary<int, int>(); 
     replaceable.Add(4, 1); 
     replaceable.Add(7, 5); 
     replaceable.Add(13, 11); 

     string input = String.Empty; 
     OpenFileDialog pfdg = new OpenFileDialog(); 
     if (pfdg.ShowDialog() == DialogResult.OK) 
     { 
      input = pfdg.FileName; 
     } 
     // I placed the result into the another file called result.txt. You can use output path as same as input to overwrite the file. 
     ReplaceLines(replaceable, input, @"C:\Users\Wallstrider\Documents\Visual Studio 2010\Projects\result.txt", 9); 
    } 
    /// <summary> 
    /// Replaces lines of the file depends on 9th item is exist. 
    /// </summary> 
    /// <param name="replaceable">Rows incides to replace.</param> 
    /// <param name="input_path">Path to the input file.</param> 
    /// <param name="output_path">Path to the output file.</param> 
    /// <param name="has_value_index">Index of a split data value in the row.</param> 
    private void ReplaceLines(Dictionary<int, int> replaceable, string input_path, string output_path, int has_value_index) 
    { 
     if (File.Exists(input_path)) 
     { 
      string file; 
      file = new StreamReader(input_path).ReadToEnd(); 

      string[] lines = file.Split(new char[] { '\n' }); 
      List<string[]> split_data = new List<string[]>(); 

      for (int i = 0; i < lines.Length; i++) 
       split_data.Add(lines[i].Split(',')); 

      List<int> allowed_for_replace_indices = new List<int>(); 
      List<int> not_allowed_for_replace_indices = new List<int>(); 

      // Check if the row has the value of 9th item then we are allowed to replace rows. 
      for (int i = 1; i < split_data.Count; i++) 
      { 
       if (split_data[i][has_value_index] != String.Empty) 
        allowed_for_replace_indices.Add(i); 
       else 
        not_allowed_for_replace_indices.Add(i); 
      } 
      List<int> rows_replaced = new List<int>(); 
      List<int> rows_not_replaced = new List<int>(); 
      // Loop through our replaceable indices dictionary. 
      for (int i = 0; i < replaceable.Count; i++) 
      { 
       int key = replaceable.ElementAt(i).Key; 
       int value = replaceable.ElementAt(i).Value; 
       // if both rows have 9th item then we can start replacement. 
       if (allowed_for_replace_indices.Contains(key) && allowed_for_replace_indices.Contains(value)) 
       { 
        string temp = lines[value]; 
        lines[value] = lines[key]; 
        lines[key] = temp; 
        rows_replaced.Add(key); 
        rows_replaced.Add(value); 
       } 
       else 
       { 
        rows_not_replaced.Add(key); 
        rows_not_replaced.Add(value); 
       } 
      } 

      using (StreamWriter sw = new StreamWriter(output_path)) 
      { 
       for (int i = 0; i < lines.Length; i++) 
        sw.WriteLine(lines[i]); 
       sw.Flush(); 
       sw.Close(); 
      } 

      MessageBox.Show("Rows replaced: " + String.Join("; ", rows_replaced.ToArray()) + " .\nRows not replaced: " + String.Join("; ", rows_not_replaced.ToArray()) + ".\nComplete."); 
     } 
    } 

下面的代碼將取代(我從零計數)4,1; 7,5; 13,11;如果我正確地理解了你的邏輯,每行都有第9項。

+0

這裏是[鏈接](https://onedrive.live.com/#cid=59802AFBA7F9048E&id=59802AFBA7F9048E%21110)你可以下載項目到測試代碼。存檔稱爲WindowsFormsApplicationHelp1。 – Wallstrider

+0

@Wallstrider ....非常感謝您的幫助。它工作完美。另一件事是,如果我不知道哪一行需要根據每行的第9項(這意味着它是否是一個大文本文件!!!)進行替換。再次非常感謝你....我得到另一種方式來思考我的問題與您的解決方案.....謝謝 – islat

0

要強制你的代碼的工作只需更換:

if (SplitText[9] == SplitData[9] && SplitText[11] == null) 

要:

if (SplitText[9] == SplitData[9] && SplitText[11] == String.Empty) 

由於SplitText [11]在您共享的文件內的所有情況下都不會爲空。

相關問題