2015-09-14 46 views
1

我有一個Windows窗體應用程序,我想從test.txt複製數據,文本文件在test2.txt。在test.txt的我的下一個行:我如何獲取文本並放入另一個文本文件?

Licfile="C:\temp\lic.lic" 
Output="C:\temp\out.log" 
Title="name" 

test2.txt我:

outlog= 
license= 
lmgr_files= 
license_path= 

,我想有這樣的事情

outlog="C:\temp\out.log" 
license_path="C:\temp\lic.lic" 
lmgr_files=false 
license=true 

正如你可以看到我需要一個沒有提及行數的代碼,並且字段的名稱是不同的。爲此,我試過這段代碼:

private void Install_Click(object sender, EventArgs e) 
{ 

    string path = AppDomain.CurrentDomain.BaseDirectory.ToString(); 
    var link = File.ReadLines(path + "test.txt").ToArray(); 
    foreach (var txt in link) 
    { 
     if (txt.Contains("Output=")) 
     { 
      var outputPath = txt.Split('=')[1]; 
      if (File.Exists(path + "test2.txt")) File.AppendAllText(path + "test2.txt", outputPath); 
      else 
      { 
       File.AppendAllText(path + "test2.txt", "outlog =" + outputPath); 
      } 
     } 
     else if (txt.Contains("Licfile=")) 
     { 
      var LicFilePath = txt.Split('=')[1]; 
      if (File.Exists(path + "test2.txt")) File.AppendAllText(path + "test2.txt", LicFilePath); 
      else 
      { 
       File.AppendAllText(path + "test2.txt", "license_path =" + LicFilePath); 
      } 
     } 
    } 
} 

但這無法完成所有的任務,當我的test2.txt運行出現:

outlog= 
license_path="C:\temp\out.log""C:\temp\lic.lic" 
lmgr_files= 
license= 

我怎樣才能使這個正常工作,它的不是一個簡單的方法,要做到這一點?

在我的代碼我用這個代碼來獲取Title,放在一個框架, 的TEXT,但我不知道如何與兩個文本文件中使用

this.Text = File.ReadLines(link) 
       .First(x => x.StartsWith("Title=\"")) 
       .Split(new[] { '=', '"' }, StringSplitOptions.RemoveEmptyEntries)[1]; 
+1

如果你要只是'.ToArray()''你ReadLines',那麼你可能也只是使用'ReadAllLines'代替。但在這種情況下,因爲您只是遍歷'link',所以放棄'.ToArray()',並使用'ReadLines'返回的'IEnumerable '。 – crashmstr

回答

0

我覺得沒有必要for循環。如果你只有這個靜態數據。試試這個:

   //Started from array of strings - (you may read from file and put in the arrays) 

       string path = AppDomain.CurrentDomain.BaseDirectory; 
       string[] text1 = System.IO.File.ReadAllLines(path + "test.txt"); 
       string[] text2 = System.IO.File.ReadAllLines(path + "test2.txt"); 

       text2[0] = text2[0] + text1.Where(x => x.Split('=')[0] == "Output").FirstOrDefault().Split('=')[1]; 
       text2[1] = text2[1] + text1.Where(x => x.Split('=')[0] == "Licfile").FirstOrDefault().Split('=')[1];  
       text2[2] = text2[2] + "false"; 
       text2[3] = text2[3] + "true"; 

       //Now the result variable will contain the final text, you can write in a file. 
       string result = string.Join(Environment.NewLine, text2); 
+0

是的,但是,如果我修改行的順序,我需要修改代碼,我不想這樣做。我想要這樣的東西this.Text = File.ReadLines(鏈接) .First(x => x.StartsWith(「Title = \」「))..... – ben

+0

現在你添加了另一個關鍵'標題'?在原文中提到的任何一個文件中的文字? –

+0

那個標題對test2.txt來說並不重要,這就是我沒有提到的原因 – ben

0

你的問題大部分來自於使用File.AppendAllText(),這增加了新的生產線,以這是不是你彷彿被希望做的文件。

當你操縱這些文件時,你應該重建整個文件。唯一的反對意見是處理大文件......但事實並非如此。

讀取第二個文件的輸入參數,並用第一個文件的輸入擴展它們,然後將整個結果寫入文件。

編輯:編輯由於新的信息提供

//I have no IDE at the moment so it's pseudo C# 
//Have you tried something like this? 
string path = AppDomain.CurrentDomain.BaseDirectory.ToString(); 
var linkFirstFile = File.ReadLines(path + "test.txt").ToArray(); 
var linkSecondFile = File.ReadLines(path + "test2.txt").ToArray(); 

var mapping = new Dictionary<string, string>(){ 
    {"outlog=", "Output"}, 
    {"license=", ""}, 
    {"lmgr_files=", ""}, 
    {"license_path=", "Licfile"}, 
}; 

foreach(var part in linkSecondFile) 
{ 
    part += linkFirstFile.FirstOrDefault(x => x.StartsWith(mapping[part])) 
} 

File.WriteAllLines(path2, linkSecondFile); 
+0

我編輯了我的文章 – ben

+0

...和?這個標題來自哪裏?這是很難給你一個答案的第一位...但如果你添加這樣的外國元素,它是越來越難... –

+0

我的數據不是訂單,我提到我不想要把行數 – ben

相關問題