Many FileStream's attached to one file - c#

Like in title:
FileStream fs = new FileStream ("test.mkv", FileMode.Open);
FileStream fs1 = new FileStream ("test.mkv", FileMode.Open);
It throw an error: The process cannot access the file '...' because it is being used by another process.
But why, if it is opened for read only (?). If not, how to open file as read only?

You need to specify that you're opening it read only and that you're sharing it.
var fs1 = new FileStream("test.mkv", FileMode.Open, FileAccess.Read, FileShare.Read);
var fs2 = new FileStream("test.mkv", FileMode.Open, FileAccess.Read, FileShare.Read);

Related

NPOI 2.6.0-rc-3 with .net framework 4.8.1 for updating .xlsx - losses charts in excel file

I use NPOI 2.6.0-rc-3 with .net framework 4.8.1 for updating .xlsx file. Excel file includes charts with it. excel file get crashed and after saving. After recovering the same file it losses charts in excel.
Used the following code.
XSSFWorkbook wb1 = null;
using (var file = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
{
wb1 = new XSSFWorkbook(file);
file.Close();
//Updated the cell values here
using (var file2 = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
{
wb1.Write(file2);
file2.Close();
}
}
Please help regrading this matter
Try to modify the cells out of the using block (you dont need to keep the file open to modify the IWorkbook) and then save it using a diferent stream:
IWorkbook wb1 = null;
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
wb1 = new XSSFWorkbook(file);
}
//Updated the cell values here
using (FileStream fileWrite = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
wb1.Write(fileWrite);
}

Read file that's already used by another process

I have a C# app that tries to read a log file which is being written to by another app. When I try to read the file, I get IOException
"The process cannot access the file ... because it is being used by
another process."
What I tried using so far are the following, but none of them fix the problem
var log = File.ReadAllText(logPath);
var stream = new FileStream(logPath, FileMode.Open);
using (var stream = File.Open(logPath, FileMode.Open))
{
}
try this:
FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);
while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
// Your code here
}
// Clean up
logFileReader.Close();
logFileStream.Close();
edited with MethodMan's suggestions
using(FileStream logFileStream = new FileStream(#"c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using(StreamReader logFileReader = new StreamReader(logFileStream))
{
string text = logFileReader.ReadToEnd();
// Your code..
}
}
You can do nothing, if the "another app" does not use Share.Read while creating/opening the file.

Coludn't acces the XML file in Visual Studio c#

When executing the code below it shows an error at opening the file
it says the file dict.xml is already in use by some process
It shows error here
FileStream fs = new FileStream("dict.xml", FileMode.Open, FileAccess.Read);
FileStream fs = new FileStream("dict.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagName("dict");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
w[i] = xmlnode[i].ChildNodes.Item(0).InnerText;
m[i] = xmlnode[i].ChildNodes.Item(1).InnerText;
}
If something else has a lock on the file you should still be able to read it if you specify FileShare.ReadWrite:
using (FileStream fs = new System.IO.FileStream("dict.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite) {

Populating xml using c# error

I am populating an xml document using c#. If I populate it once then everything is ok but when I try to repopulate it a second time (without closing the program) I get an error message and rubish gets written to the bottom of the xml file.
I'm sure I've found the answer to this question before but I can't find it.
I'm sure its something to do with the fact that I'm not closing something down after updating the xml document or something but I can't remember what exactly I have to close down.
Sorry I hope you all understand. I find it difficult to explain.
Code:
using (FileStream READER = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
System.Xml.XmlDocument Template = new System.Xml.XmlDocument();// Set up the XmlDocument //
Template.Load(READER); //Load the data from the file into the XmlDocument //
//**********Grab nodes to be written to********
using (FileStream WRITER = new FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
//Set up the filestream (READER) //
//Write the data to the filestream
Template.Save(WRITER);
You read and write in the same file fpath
You have to close the reader after Template.Load(READER);
using (FileStream READER = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
System.Xml.XmlDocument Template = new System.Xml.XmlDocument();// Set up the XmlDocument //
Template.Load(READER); //Load the data from the file into the XmlDocument //
}
//**********Grab nodes to be written to********
using (FileStream WRITER = new FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
//Set up the filestream (READER) //
//Write the data to the filestream
Template.Save(WRITER);
...
}

Files to binary stream

I want to fetch the files from the folder and convert to binary stream. How is it possible using ASP.Net with C# ?
A FileStream will do the job:
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// work with fs
}

Categories