English 中文(简体)
读书后不得写
原标题:Cannot write to file after reading
  • 时间:2011-07-15 18:05:12
  •  标签:
  • c#
  • file-io

In the following code I get the error "stream was not writable":

class Class1
{
    private static void Main()
    {
        FileStream fs = new FileStream("C:\fFile.txt", 
                              FileMode.OpenOrCreate, 
                              FileAccess.ReadWrite, 
                              FileShare.ReadWrite);

        StreamReader r = new StreamReader(fs);
        string t = r.ReadLine();
        r.Close();
        Console.WriteLine(t);

        StreamWriter w = new StreamWriter(fs);
        w.WriteLine("string");
        w.Flush();
        w.Close();
        fs.Close();

    }
}    

这一错误出现在<代码>上。 页: 1

为什么如此?

最佳回答

页: 1

http://www.ohchr.org。

因此,你试图写字的行文是无效的,你需要重新打开溪流并重开档案。

问题回答
r.Close(); 

你读了话后,你就存在问题。 接近(接近)的方法接近下游。

你不得不重开档案,因为阅读已经结束:

FileStream fs = new FileStream("C:\test.txt", 
                        FileMode.OpenOrCreate, 
                        FileAccess.ReadWrite, 
                        FileShare.ReadWrite);
using (StreamReader r = new StreamReader(fs))
{
    string t = r.ReadLine();
    r.Close();
    Console.WriteLine(t);
}

fs = new FileStream("C:\test.txt", 
            FileMode.OpenOrCreate, 
            FileAccess.ReadWrite, 
            FileShare.ReadWrite);

using (StreamWriter w = new StreamWriter(fs))
{
    w.WriteLine("string");
    w.Flush();
    w.Close();
}
fs.Close();

Dont close the first StreamWriter, it will close the underlying stream. And use using statements as Oscar suggests.

using (FileStream fs = new FileStream("C:\temp\fFile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
    StreamReader r = new StreamReader(fs);
    string t = r.ReadLine();

    Console.WriteLine(t);

    StreamWriter w = new StreamWriter(fs);
    w.WriteLine("string");
    w.Close();
    r.Close();
    fs.Close();
}

不要关闭PigReader。 仅谈以下内容,并将奏效。

 r.Close();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签