How to Both Read and Write a File in C#

Read and write to a file in the same stream

Just Flush your changes to file, Have sw.Flush(); before closing the stream. like:

string filePath = "test.txt";
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);
newString = sr.ReadToEnd() + "somethingNew";
sw.Write(newString);
sw.Flush(); //HERE
fs.Close();

You may see this post simultaneous read-write a file in C# (open multiple streams for reading and writing)

Read and Write to File at the same time

Simply holding a FileStream open with exclusive (not shared) access will prevent other processes from accessing the file. This is the default when opening a file for read/write access.

You can 'overwrite' a file that you currently hold open by truncating it.

So:

using (var file = File.Open("storage.bin", FileMode.Open))
{
// read from the file

file.SetLength(0); // truncate the file

// write to the file
}

the method should be absolute safe, since the program should run on 2000 devices at the same time

Depending on how often you're writing to the file, this could become a chokepoint. You probably want to test this to see how scalable it is.

In addition, if one of the processes tries to operate on the file at the same time as another one, an IOException will be thrown. There isn't really a way to 'wait' on a file, so you probably want to coordinate file access in a more orderly fashion.

Write and read from same file

I think you just need to use the Using statement while read and write.

Since the StreamWriter and StreamReader classes inherits Stream, which is implements the IDisposable interface, the example can use using statements to ensure that the underlying file is properly closed following the write or read operations.

using (StreamWriter sw = new StreamWriter(F))
{
//Write Logic goes here...
}

using (StreamReader file = new StreamReader(F2))
{
//Read Logc goes here...
}

The Using Statement:

An using statement is translated into three parts: acquisition, usage,
and disposal. Usage of the resource is implicitly enclosed in a try
statement that includes a finally clause. This finally clause disposes
of the resource. If a null resource is acquired, then no call to
Dispose is made, and no exception is thrown.

simultaneous read-write a file in C#

Ok, two edits later...

This should work. The first time I tried it I think I had forgotten to set FileMode.Append on the oStream.

string test = "foo.txt";

var oStream = new FileStream(test, FileMode.Append, FileAccess.Write, FileShare.Read);
var iStream = new FileStream(test, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

var sw = new System.IO.StreamWriter(oStream);
var sr = new System.IO.StreamReader(iStream);
var res = sr.ReadLine();
res = sr.ReadLine();
sw.WriteLine("g");
sw.Flush();
res = sr.ReadLine();
res = sr.ReadLine();
sw.WriteLine("h"); sw.Flush();
sw.WriteLine("i"); sw.Flush();
sw.WriteLine("j"); sw.Flush();
sw.WriteLine("k"); sw.Flush();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();

Easiest way to read from and write to files

Use File.ReadAllText and File.WriteAllText.

MSDN example excerpt:

// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

...

// Open the file to read from.
string readText = File.ReadAllText(path);

Read and write the same file c#

Use

using(FileStream fs = new FileStream(@"login.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
...
}

FileAccess.ReadWrite allows you to both read and write on the stream and FileShare.ReadWrite allows you to have more than one reader OR writer to the file

So in theory you can have two of these if you wish

PS: I would not use a textfile to do the login credentials of anything.



Related Topics



Leave a reply



Submit