In FileStream.ReadAsync we still have to use split logic? - c#

My Current Project download manger tool have code like this(For downloading large files from file share, on shcedule) :
using (var destinationFileStream = new FileStream(sourceFilename, FileMode.OpenOrCreate, FileAccess.Write))
{
using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, buffer.Length, false))
{
while (true)
{
if (file.Read(buffer, 0, buffer.Length) <= 0)
break;
destinationFileStream.Write(buffer, 0, buffer.Length);
....
... Bandwidth throttling Code
....
}
}
}
if i convert this to FileStream.ReadAsync code , still i have to use split file to buffer size logic and download or this is handled internally by .net ?

Related

c# Two FileStreams write to text file but just one affect file

below code:
var fs = new FileStream(#"C:\Users\Michał\Desktop\tools\test.txt",
FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
var fs2 = new FileStream(#"C:\Users\Michał\Desktop\tools\test.txt",
FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
int a = 0;
while (a < 3)
{
byte[] info = new UTF8Encoding(true).GetBytes("DEF_");
byte[] info2 = new UTF8Encoding(true).GetBytes("abc_");
fs.Write(info, 0, info.Length);
Thread.Sleep(100);
fs2.Write(info2, 0, info2.Length);
Thread.Sleep(1000);
++a;
}
fs.Close();
fs2.Close();
Why result is that in a file there is just "abc_abc_abc" ?
FileShare.ReadWrite means for me other processes/threads can write to this file in the same time in FileStream ctor call.
You can achieve the desired behavior as follows:
using (var fs = new FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
using (var fs2 = new FileStream("test.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
byte[] info = new UTF8Encoding(true).GetBytes("DEF_");
byte[] info2 = new UTF8Encoding(true).GetBytes("abc_");
for (int i = 0; i < 3; i++)
{
fs.Seek(0, SeekOrigin.End);
fs.Write(info, 0, info.Length);
fs.Flush();
fs2.Seek(0, SeekOrigin.End);
fs2.Write(info2, 0, info2.Length);
fs2.Flush();
}
}
Before writing, each pointer of stream must be positioned at the end. This is done using the Seek method.
After writing, you need to flush the buffer to disk. This ensures that the stream is in the correct state before starting the next write. To do this, use the Flush method.
When you create a stream in its constructor, you can specify the FileOptions.WriteThrough. According to its description, the intermediate buffer should not be used. However, it still doesn't work without the Flush method. Perhaps experts will explain the reason.

Error "file in use" when playing aac while appending bytes to aac

var outputfile = "outputfile.aac";
var fileList = new List<string>
{
"1.aac",
"2.aac",
"3.aac",
"4.aac",
"5.aac",
"6.aac"
};
foreach (var file in fileList)
{
using (var stream = new FileStream(outputfile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
var bytes = File.ReadAllBytes(file);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
}
I try to join aac files in fileList into outputfile. when writing file "3.aac" to outputfile.aac, I open outputfile with MediaPlayer, then the FileStream returns exception "file in use", even FileShare mode is ReadWrite, it means that other processes can read and write to the file. So where is the reason?
However, in this case output file is not blocked
Stream s = resp.GetResponseStream();
var fs = File.Exists(outputFile) ? new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
: new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] buffer = new byte[4096];
while (s.CanRead)
{
int bytesRead = s.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
}
P/s: I found that the reason because it's aac file
When you use FileShare.ReadWrite you allow subsequent opening of the file in read or write mode.
MediaPlayer then opens the file with FileShare.Read on its side, denying you write access to the file.

C# write an uploaded file to a UNC with FileStream, read it later sometimes doesn't work

I've got a rare case where a file cannot be read from a UNC path immediately after it was written. Here's the workflow:
plupload sends a large file in chunks to a WebAPI method
Method writes the chunks to a UNC path (a storage server). This loops until the file is completely uploaded.
After a few other operations, the same method tries to read the file again and sometimes it cannot find the file
It only seems to happen after our servers have been idle for a while. If I repeat the upload a few times, it starts to work.
I thought it might be a network configuration issue, or something to do with the file not completely closing before being read again.
Here's part of the code that writes the file (is the filestream OK in this case?)
SaveStream(stream, new FileStream(fileName, FileMode.Append, FileAccess.Write));
Here's SaveStream definition:
private static void SaveStream(Stream stream, FileStream fileStream)
{
using (var fs = fileStream)
{
var buffer = new byte[1024];
var l = stream.Read(buffer, 0, 1024);
while (l > 0)
{
fs.Write(buffer, 0, l);
l = stream.Read(buffer, 0, 1024);
}
fs.Flush();
fs.Close();
}
}
Here's the code that reads the file:
var fileInfo = new FileInfo(fileName);
var exists = fileInfo.Exists;
It's the fileInfo.Exists that is returning false.
Thank you
These kind of errors are mostly due to files not closed yet.
Try passing the fileName to SaveStream and then use it as follows:
private static void SaveStream(Stream stream, string fileName)
{
using (var fs = new FileStream(fileName, FileMode.Append, FileAccess.Write))
{
var buffer = new byte[1024];
var l = stream.Read(buffer, 0, 1024);
while (l > 0)
{
fs.Write(buffer, 0, l);
l = stream.Read(buffer, 0, 1024);
}
fs.Flush();
} // end of using will close and dispose fs properly
}

How to access and save embedded resources to the local disk in C#?

I have a set of Folders(in Resources) which contains the Configuration files,I need to save this folder in the User's path.(if they don't have) for this reason only i added the Folders in Resources. Do anyone have any idea on how to save the Files in the Resources.
using (System.IO.FileStream fs = new System.IO.FileStream(pathConfigurationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
byte[] data = Properties.Resources.YourConfigurationFile;
fs.Write(data, 0, data.Length);
}
var data = Properties.Resources.ResourceName;
using(var stream = new FileStream(file, FileMode.Create))
{
stream.Write(data, 0, data.Count() - 1);
stream.Flush();
}

HTTP Error 401.3 on created .gz file

I have method to compress file with GZip:
public static void CompressFile(string filePath)
{
string compressedFilePath = Path.GetTempFileName();
using (FileStream compressedFileStream = new FileStream(compressedFilePath, FileMode.Append, FileSystemRights.Write, FileShare.Write, BufferSize, FileOptions.None))
{
GZipStream gzipStream = new GZipStream(compressedFileStream, CompressionMode.Compress);
using (FileStream uncompressedFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
int offset = 0;
while (true)
{
byte[] buffer = new byte[offset + BufferSize];
int bytesRead = uncompressedFileStream.Read(buffer, offset, BufferSize);
if (bytesRead == 0)
break;
gzipStream.Write(buffer, offset, bytesRead);
offset += bytesRead;
}
}
gzipStream.Close();
}
File.Delete(filePath);
File.Move(compressedFilePath, filePath);
}
My problem is, that on testing server (Win08 R2) it creates file and it can be downloaded via browser, but on webhosting server (older Win08 R1) it also creates file, but if i want to download it, access denied exception is thrown.
Differences are in file permission. On R2 server has access to file Application pool identity (e.g. "MyWebSite"), but on R1 only IIS_IUSRS with "Special permission".
Ensure you have a MIME type added for the .gz extension in IIS configuration. I think this may cause the issue you are referring to.

Categories