我有一個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];
如果你要只是'.ToArray()''你ReadLines',那麼你可能也只是使用'ReadAllLines'代替。但在這種情況下,因爲您只是遍歷'link',所以放棄'.ToArray()',並使用'ReadLines'返回的'IEnumerable'。 –
crashmstr