The FileStream class is derived from Stream class. This class can be used for reading from and writing to files such as bytes, characters, strings, and other data-type values to a file. Other class I have used in this sample is StreamWriter. The StreamWriter class's Write and WriteLine members write to a file.
In this sample code, I have used FileStream class to create a text file and StreamWrite to write text to the text file.
namespace mcFileStream
{
using System;
using System.IO;
public class mcLogFile
{
public static void Main(String[] args)
{
// Create a text file C:\temp\mcb.txt
FileStream fs = new FileStream(@"c:\temp\mcb.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
// Write to the file using StreamWriter class
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.Write(" File Write Operation Starts : ");
m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
m_streamWriter.WriteLine(" First Line : Data is first line \n");
m_streamWriter.WriteLine(" This is next line in the text file. \n ");
m_streamWriter.Flush();
// Read from the file using StreamReader class
// StreamReader m_streamReader = new StreamReader(fs);
// string str = m_streamReader.ReadLine();
}
}
}
Joe WilsonPosted Dec 28, 2015, 2:10 AM
Well, Thank you very much.
berat beratPosted Dec 16, 2014, 3:09 AM
hey i am using FileStream in multithreaded app. i have this line code lock (objLocker){using (FileStream fs = new FileStream(filePath,", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) }} i took exxeption it is used by another process how can i fix that
Mahesh ChandPosted May 30, 2012, 10:24 AM
@Nidhi, you need to make sure you have read/write permissions on mcb.txt file and the file is there. Also make sure to Close the file everytime you open it. I am going to update this article and put Open and Close within a try..catch block.
Mahesh ChandPosted May 30, 2012, 10:23 AM
Ahmed, If you would have used the search feature, you could have found this. This article was posted 12 years ago.
Ahmed AldabbaghPosted May 17, 2012, 7:03 PM
Hi, I think it was really waste of time as my question was posted in 2010 and the response came after two years it is the longest response I'v ever seen. Thanks mate
nidhi bishteditedPosted May 17, 2012, 3:08 PMEdited May 17, 2012, 3:09 PM
FileStream fs = new FileStream(@"c:\\mcb.txt", FileMode.Create, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);Console.WriteLine("Please enter your information");string str = Console.ReadLine();sw.WriteLine(str);sw.Close();fs.Close(); Console.ReadLine(); My program give me an error "Access to the path 'c:\mcb.txt' is denied." Pls tell me what to do if possible so send me ans at my accout i.e. [email protected] pls reply as soon as possible
Lennie KuahPosted Sep 26, 2010, 6:23 PM
Hi Mahesh, Regarding this coding: What Project Reference should be used ? FileStream : Read or Write Data Streams
Ahmed AldabbaghPosted Aug 29, 2010, 10:29 AM
Hi I wounder how can i get the result of my running prog. to be printed in a (notepad) file instead of the screen.in C++
danial haziqPosted May 6, 2009, 4:37 AM
hi..first of all, my coding is about to write or embed template to the word document..but after embed the doucument cannot be open but then it happens vice versa as the document can be open.. here i attach my coding also private void SaveButton_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog(); save.Filter = "Fingerprint Template File (*.fpt)|*.fpt"; if (save.ShowDialog() == DialogResult.OK) { using (FileStream fs = File.Open(save.FileName, FileMode.Append, FileAccess.Write)) { Template.Serialize(fs); fs.Close(); } } { //embed the file //SaveFileDialog save = new SaveFileDialog(); save.Filter = "Word Document (*.doc)|*.doc|(*.docx)|*.docx"; if (save.ShowDialog() == DialogResult.OK) { using (FileStream fs = File.Open(save.FileName, FileMode.Append, FileAccess.Write)) { fs.Write(Template.Bytes, 0, Template.Size); fs.Close(); // MD5Hash.CheckSum(open.FileName)) this.WindowState = FormWindowState.Minimized; // FileStream fsa = File.Open(save.FileName, FileMode.Append, FileAccess.Write); } }
Naka KumaPosted Nov 13, 2007, 11:42 PM
Hi, Is FileStream class can be done with this situation?
Nik HatPosted Sep 22, 2007, 2:06 PM
Hi, I want to choose .csv file, read it and do validation for each of the columns for string/Date/Numeric. Highlight data fields for records that have errors. Any help, where to start from, would be highly appreciated. Thanks, Taps
venkat konathalaPosted Jul 5, 2007, 1:28 PM
Hi I am subbu and I am doing a simple application(window) using Oracle database. I have few search results being displayed in datagrid and need the results to be exported to excel sheet. Can you please help me out in this.