该程序的功能是获取各个控件中的内容并存privatevoidbutton4_Click(objectsender,EventArgse){stringfilename=@... 该程序的功能是获取各个控件中的内容并存
private void button4_Click(object sender, EventArgs e)
{
string filename = @"c:\config.txt";
int j = 0;

System.IO.StreamReader sr = new System.IO.StreamReader(filename);
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename,true,Encoding.Default);
string line;
string tablename = textBox3.Text;
string tagname = listBox1.Text;
string starttime = dateTimePicker1.Text;
string endtime = dateTimePicker2.Text;
string interval = textBox1.Text;
string xlspath = textBox2.Text;
string[]a={tablename,tagname,starttime,endtime,interval,xlspath};

while ((line = sr.ReadLine())!=null)
{

sw.WriteLine(line + a[j]);
j++;
}

sr.Close();

sw.Close();
}
入到txt文件的指定行的末尾,但是每次运行输入数据总是报错(如标题所示),请问哪里有问题
private void button4_Click(object sender, EventArgs e)
{
string filename = @"c:\config.txt";
int j = 0;

System.IO.StreamReader sr = new System.IO.StreamReader(filename);
string line;
string tablename = textBox3.Text;
string tagname = listBox1.Text;
string starttime = dateTimePicker1.Text;
string endtime = dateTimePicker2.Text;
string interval = textBox1.Text;
string xlspath = textBox2.Text;
string[]a={tablename,tagname,starttime,endtime,interval,xlspath};

//此处你先将内容读出来
while ((line = sr.ReadLine())!=null)
{
a[j]=a[j]+line;
j++
}
sr.Close();
sr.Dispose();

//循环写入,覆盖源文件
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename,false,Encoding.Default);
foreach(var data in a)
{
sw.WriteLine(data);
}
sw.Close();
sw.Dispose();
}

按你的改后又出现这个,也是我经常碰到的错误!!

出现这个问题的原因在于你的行数大于了6行,致使j>=6了,然后数组a的最大索引为5,所以造成数组越界。你调试跟踪一下。

tablename:t1 t1 t1 t1
tagname:tag1 tag1 tag1 tag1
starttime:start1 start1 start1 start1
endtime:end1 end1 end1 end1
interval:in1 in1 in1 in1
xlspath:path1 path1 path1 path1
这是我执行后的文件内容,没有错误啊!你要保证文件只有6行
a[j] = line + a[j];这样写才能保证写到新的内容写到行的末尾。
哦,你加断点,看看哪里报的异常。
应该这样写那就,
string filename = @"c:\config.txt";
int j = 0;

System.IO.StreamReader sr = new System.IO.StreamReader(filename);

string line;
string tablename = textBox3.Text;
string tagname = listBox1.Text;
string starttime = dateTimePicker1.Text;
string endtime = dateTimePicker2.Text;
string interval = textBox1.Text;
string xlspath = textBox2.Text;
string[]a={tablename,tagname,starttime,endtime,interval,xlspath};
sr.Close();
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename,true,Encoding.Default);while ((line = sr.ReadLine())!=null)
{

sw.WriteLine(line + a[j]);
j++;
}
sw.Close();
这样应该是好使了。要是再不好使你加断点看看哪里报的异常
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename,true,Encoding.Default); 这这句话报异常,还是不行呀
while ((line = sr.ReadLine())!=null)
这是要干什么?
你需要读取之后关闭流,然后再写。
不能读和写的流同时打开,因为他们都是以独占的方式打开
就是一行一行的读取文件内容吧
可以像楼下说的使用list存储读出来的东西,
但不能以这种方式边读边写,写之前要将读的流关闭。
你可以将读和写分开,因为读写不能同时进行,修改代码如下
string filename = @"D:\config.txt";
int j = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
string line;
string tablename = textBox3.Text;
string tagname = listBox1.Text;
string starttime = dateTimePicker1.Text;
string endtime = dateTimePicker2.Text;
string interval = textBox1.Text;
string xlspath = textBox2.Text;
string[] a = { tablename, tagname, starttime, endtime, interval, xlspath };
string[] b = new string[6];
while ((line = sr.ReadLine()) != null)
{
b[j] = line;
j++;
}
sr.Close();
System.IO.File.Delete(filename);
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, true, Encoding.Default);
for (int i = 0; i < a.Length; i++)
{
sw.WriteLine(b[i] + a[i]);
}
sw.Close();

全部读完到一个list里面,然后修改内容,最后你再覆盖写到文件中。

 System.IO.StreamReader sr = new System.IO.StreamReader(filename);
         List<string> strList=new  List<string>();
            string line;
            string tablename = textBox3.Text;
            string tagname = listBox1.Text;
            string starttime = dateTimePicker1.Text;
            string endtime = dateTimePicker2.Text;
            string interval = textBox1.Text;
            string xlspath = textBox2.Text;
            string[]a={tablename,tagname,starttime,endtime,interval,xlspath};

while (!sr.EndOfStream)
                {
                   strList.Add(sr.ReadLine());
                   
                }
            
                sr.Close();
                   System.IO.StreamWriter sw = new System.IO.StreamWriter(filename,true,Encoding.Default);
                   for(int j=0;j<strlist.count;j++)
                   {
                    sw.WriteLine(line + a[j]);
                   }
                sw.Close();