private void button1_Click(object sender, EventArgs e)
        {
            FileStream fi;
            try
            {
                fi = File.Create(textBox1.Text);            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            byte[] content = new UTF8Encoding(true).GetBytes(textBox2.Text);
            try
            {
                fi.Write(content, 0, content.Length);
                fi.Flush();
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);            }
            finally
            {
                fi.Close();
            }
错误提示是没有跟fi局部变量赋值!可是上面有跟他赋值啊,我不懂了

解决方案 »

  1.   

    你那个不行,赋值操纵在{}之间了,一开始就初始化FileStream fi = new FileStream("", FileMode.Open)
      

  2.   

    这样初始化好像不行呢,这跟try catch里面的语句有没有冲突?
      

  3.   

    就在你这段代码的第一行就初始化 fi ,后面try...catch都能用
      

  4.   


    FileStream fi = null; 
      

  5.   

    作用域问题,你的赋值操作本身可能失败,在作用域外部使用时,不知道try内部的执行情况的。
      

  6.   

    private void 修改日记(string 输出内容)
            {
                String 保存路径 = @System.Environment.CurrentDirectory + "\\修改日记";
                if (Directory.Exists(保存路径))
                {
                    String 保存文件名 = @保存路径 + "\\" + 时间.ToLongDateString() + ".txt ";
                    FileInfo 文件 = new FileInfo(保存文件名);
                    if (!文件.Exists) { FileStream 创建或覆盖 = File.Create(保存文件名); 创建或覆盖.Flush(); 创建或覆盖.Close(); }
                    //{ 文件.Create(); }FileStream 创建只写文件 = 文件.OpenWrite(); 创建只写文件.Close();
                    //StreamWriter 写入 = File.AppendText(保存文件名);//以可以追加文本的方式打开文件流
                    StreamWriter 写入 = new StreamWriter(保存文件名, true);//以可以追加文本的方式打开文件流
                    写入.WriteLine(输出内容); 写入.Flush(); 写入.Close();
                }
                else
                {
                    Directory.CreateDirectory(保存路径);
                    String 保存文件名 = @保存路径 + "\\" + 时间.ToLongDateString() + ".txt ";
                    FileStream 创建或覆盖 = File.Create(保存文件名);
                    创建或覆盖.Flush(); 创建或覆盖.Close();
                    StreamWriter 写入 = new StreamWriter(保存文件名);//不可追加文本
                    写入.WriteLine(输出内容); 写入.Flush(); 写入.Close();
                }
            }
    就当参考吧