StreamWriter对象没有';根本不起作用

本文关键字:不起作用 对象 StreamWriter | 更新日期: 2023-09-27 18:26:36

此代码在SaveFileDialog 中运行良好

private void buttonSaveAs_Click(object sender, EventArgs e)
        {
            try
            {
                if (selectedFileInfo != null)
                {
                    // Save File
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.InitialDirectory = explorerTree2.SelectedPath; 
                    // set a default file name
                    saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(selectedFileInfo.Name) + "-COPY" + selectedFileInfo.Extension;
                    // set filters - this can be done in properties as well
                    saveFileDialog1.Filter = "HTM files (*.htm)|*.htm|HTML files (*.html)|*.html|XML files (*.xml)|*.xml|Text files (*.txt)|*.txt|All files (*.*)|*.*";
                    #region Define filter index
                    if (selectedFileInfo.Extension.Equals(".htm")) // HTM files (*.htm)|*.htm
                    {
                        saveFileDialog1.FilterIndex = 1;
                    }
                    else // All files (*.*)|*.*
                    {
                        saveFileDialog1.FilterIndex = 5;
                    }
                    #endregion
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
                        { 
                            sw.Write(scintilla1.Text);
                            sw.Flush();
                            sw.Close();
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

另一个没有。也没有任何错误。有线索吗?

private void buttonSave_Click(object sender, EventArgs e)
{
    try
    {
        if (selectedFileInfo != null)
        {
            using (FileStream fs = new FileStream(selectedFileInfo.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.AutoFlush = true;
                sw.Write(scintilla1.Text);                       
                sw.Flush();
                sw.Close();
            }
        }
    }
    catch (IOException ex)
    {
        MessageBox.Show(ex.Message);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

p.S.此选项不起作用

 using (FileStream fs = new FileStream(selectedFileInfo.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite))
   {
         using (StreamWriter sw = new StreamWriter(fs))
         {
               sw.AutoFlush = true;
               sw.Write(scintilla1.Text);
               sw.Flush();
               sw.Close();
         } 
}

StreamWriter对象没有';根本不起作用

我找到了正确的解决方案

其中闪烁1.文本是您必须写入的字符串selectedFileInfo是一个CCD_ 2对象。在您的情况下,闪烁1。文本可能类似于stringRichTextBox或任何其他文本编辑器控件。

  private void buttonSave_Click(object sender, EventArgs e)
        {
            if (selectedFileInfo != null)
            {
                FileStream stream = null;
                try
                {
                    stream = selectedFileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                    using (StreamWriter sw = new StreamWriter(stream))
                    {
                        sw.AutoFlush = true;
                        sw.Write(scintilla1.Text);
                        sw.Flush();
                        sw.Close();
                    }
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (stream != null)
                        stream.Close();
                }
            }
        }