初学C#,先查询出datagridview中的数据,再导出为txt,重复导出同样的数据是正常的,但改变查询的数据后就出现如标题提示的错误,代码如下,请大师指点。
using (SaveFileDialog sfdTxt = new SaveFileDialog())
{
Stream txtStream = null;
sfdTxt.Filter = "txt文本文件(*.txt)|*.txt";
sfdTxt.RestoreDirectory = true;
sfdTxt.FileName = string.Format(DateTime.Now.ToString("yyyyMMdd"));
//错误时指向下面这句。
if (sfdTxt.ShowDialog() == DialogResult.OK)
{
if ((txtStream = sfdTxt.OpenFile()) != null)
{
using (StreamWriter streamWriter = new StreamWriter(@txtStream, System.Text.Encoding.Unicode))
{
for (int i = 0; i < this.dgvSale.Rows.Count; i++)
{
streamWriter.WriteLine(this.dgvSale.Rows[i].Cells[3].Value.ToString() + " " + this.dgvSale.Rows[i].Cells[8].Value.ToString() + " " + this.dgvSale.Rows[i].Cells[10].Value.ToString());
}
streamWriter.Close();
}
}
}
else
{
return;
}
sfdTxt.Dispose();
txtStream.Close();
}
C# WinForm 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
答案:2 悬赏:80 手机版
解决时间 2021-03-05 20:39
- 提问者网友:那叫心脏的地方装的都是你
- 2021-03-04 22:19
最佳答案
- 五星知识达人网友:玩世
- 2021-03-04 23:00
using (SaveFileDialog sfdTxt = new SaveFileDialog())
{
sfdTxt.Filter = "txt文本文件(*.txt)|*.txt";
if (DialogResult.OK==sfdTxt.ShowDialog())
{
using (StreamWriter streamWriter = new StreamWriter(sfdTxt.FileName, System.Text.Encoding.Unicode))
{
for (int i = 0; i < this.dgvSale.Rows.Count; i++)
{
streamWriter.WriteLine(this.dgvSale.Rows[i].Cells[3].Value.ToString() + " " + this.dgvSale.Rows[i].Cells[8].Value.ToString() + " " + this.dgvSale.Rows[i].Cells[10].Value.ToString());
}
streamWriter.Close();
}
}
else
{
return;
}
txtStream.Dispose();
}
{
sfdTxt.Filter = "txt文本文件(*.txt)|*.txt";
if (DialogResult.OK==sfdTxt.ShowDialog())
{
using (StreamWriter streamWriter = new StreamWriter(sfdTxt.FileName, System.Text.Encoding.Unicode))
{
for (int i = 0; i < this.dgvSale.Rows.Count; i++)
{
streamWriter.WriteLine(this.dgvSale.Rows[i].Cells[3].Value.ToString() + " " + this.dgvSale.Rows[i].Cells[8].Value.ToString() + " " + this.dgvSale.Rows[i].Cells[10].Value.ToString());
}
streamWriter.Close();
}
}
else
{
return;
}
txtStream.Dispose();
}
全部回答
- 1楼网友:风格不统一
- 2021-03-04 23:47
if(ofdlinsertbools.showdialog() == dialogresult.ok)
{
string name = ofdinsertbooks.filename //全路径,包括图片名称
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯