下面这段代码一执行到红字部分,就说filePath这个路径正在被使用,不能引用,请问是什么问题应该怎么解决?
if (this.fileMode == "new")
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = ".txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
if (sfd.FileName != "")
{
if (File.Exists(sfd.FileName))
{
if (MessageBox.Show(this, "This Filename has exist,do you want to continue?", "Same Name", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
File.Create(sfd.FileName);
filePath = sfd.FileName;
}
else { return; }
}
else
{
File.Create(sfd.FileName);
filePath = sfd.FileName;
}
}
else
{
MessageBox.Show(this, "Please input filename!", "new file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
}
try
{
StreamWriter sw = new StreamWriter(filePath); if (sw != null)
{
sw.Write(this.textBox1.Text);
}
sw.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); } this.Close();
if (this.fileMode == "new")
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = ".txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
if (sfd.FileName != "")
{
if (File.Exists(sfd.FileName))
{
if (MessageBox.Show(this, "This Filename has exist,do you want to continue?", "Same Name", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
File.Create(sfd.FileName);
filePath = sfd.FileName;
}
else { return; }
}
else
{
File.Create(sfd.FileName);
filePath = sfd.FileName;
}
}
else
{
MessageBox.Show(this, "Please input filename!", "new file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
}
try
{
StreamWriter sw = new StreamWriter(filePath); if (sw != null)
{
sw.Write(this.textBox1.Text);
}
sw.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); } this.Close();
解决方案 »
- 求教DevExpress.XtraGrid.GridControl控件获取选中行某列的值
- winform一个错误,求修改
- 类中的属性的初始化顺序是怎么的?
- 助人为快乐之本:菜鸟问题:Winform关于C#实现套打功能
- 关于winform窗体的疑问
- 据说从Access2000以上开始支持的那个"假"存储过程在哪里建立?
- *******高分求一个Remoting传输文件的例子******
- 有网吧广告发布系统的源码吗
- 请教:aaa{bbb {is} ccc {0:xxx}.ccc}我想匹配"{"后面不是数字的括号对,能用正则表达式吗?
- c# excel 生成图表无法调整大小
- 如何将任务栏设置成自动隐藏,而不是隐藏
- 求一类似帮助CHM文档控件
StreamWriter sw = new StreamWriter(fs);
这样改
解决1:
注释掉两行File.Create(sfd.FileName);
后面的StreamWriter会自己创建,你这里创建多余!
解决2:
...