我想根據某些數據用文件的現有行替換文本的行。我開發了一些代碼塊,但沒有奏效。 我的文本文件是這樣的: -用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();
}
}
}
}
你可以發佈錯誤以及哪部分代碼會拋出錯誤嗎? – failedprogramming
「停止工作」不是對發生什麼的技術描述。如果你需要幫助,你需要學會像程序員一樣思考,做一些技術觀察,然後在你的問題中報告這些細節。 –
您正在創建嵌套的讀者和作家。想一想,這有道理嗎?考慮1個閱讀器和1個相同級別的作家以及與你的邏輯相匹配的複製行。 –