Search code examples
c#.netfilestreamstreamwriterwritetofile

StreamWriter object doesn't work at all


This code works fine within 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);
            }
        }

And another does not. And there NO is any error as well. Any clue?

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. This option does not work as well

 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();
         } 
}

Solution

  • I found correct solution

    where scintilla1.Text is string you have to write and selectedFileInfo is a FileInfo object. In your case scintilla1.Text might be like a string or RichTextBox or any other text editor control.

      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();
                    }
                }
            }