locked
Stream was not writable error when trying to write to a file RRS feed

  • Question

  • User-2066177896 posted

    Hi,

    This is for a winforms application. 

    I am checking if a file exists and if the file exists, i have to open it and write text to the file. I am getting the following error. 

    Error: Stream was not writable


    If File.Exists(finalPath) Then
    
                Using fs As FileStream = New FileStream(finalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                    Using tw As TextWriter = New StreamWriter(fs) '---Error here
                        tw.WriteLine(txtCID.Text)
                    End Using
                End Using
    End If

    Thanks in Advance

    Thursday, April 5, 2018 6:45 PM

All replies

  • User475983607 posted

    The posted code specifically sets the file access to read only.  Change it to ReadWrite if you want to write to the file.

    Using fs As FileStream = New FileStream(finalPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)

    Reference docs

    https://msdn.microsoft.com/en-us/library/5h0z48dh(v=vs.110).aspx

    By the way, this is an ASP.NET support forum for building websites not Windows Forms support.

    Thursday, April 5, 2018 6:53 PM
  • User-2066177896 posted

    Changing to FileAccess.ReadWrite gives another error

    "The process cannot access the file 'Path\CID.txt' because it is being used by another process."

    Thursday, April 5, 2018 7:01 PM
  • User475983607 posted

    rowter

    Changing to FileAccess.ReadWrite gives another error

    "The process cannot access the file 'Path\CID.txt' because it is being used by another process."

    Stop the other process.   Maybe reboot the system or log off an on again.

    Again, you are in the wrong support forum!

    Thursday, April 5, 2018 7:04 PM
  • User-1776422350 posted
          public string SaveHtml(string fileName,string context)
            {
                try
                {
                   
                    if (context.Length > 0)
                    {
                        //fileName = fileName + ".html";//生成最终文件名
                        FileStream fs = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("UTF-8"));
                        sw.Flush();
                        sw.BaseStream.Seek(0, SeekOrigin.Begin);
                        sw.WriteLine(context);
                        sw.Flush();
                        sw.Close();
                        return fileName;
                    }
                    else
                    {
    
                        return "生成文件失败!";
                    }
    
                }
                catch (Exception)
                {
    
                    throw;
                }
            }

     close  the   StreamWriter

      tw.Flush();

    tw.Close();

    Thursday, April 5, 2018 8:48 PM
  • User61956409 posted

    Hi rowter,

    The process cannot access the file 'Path\CID.txt' because it is being used by another process.

    As mgebhard mentioned, the error message indicates the file is being used by another process, you could try to use Process Monitor tool to find which process is using (or locking) 'Path\CID.txt', then you could manually end the process and execute your code to write content to that file.

    With Regards,

    Fei Han

    Friday, April 6, 2018 7:40 AM