CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to use C# FileStream Class

The FileStream Class represents a File in the Computer. Use the FileStream class to read from, write to, open, and close files on a file system, as well as to manipulate other file related operating system handles including pipes, standard input, and standard output. FileStream allows to move data to and from the stream as arrays of bytes. We operate File using FileMode in FileStream Class

Some of FileModes as Follows :

FileMode.Append : Open and append to a file if the file does not exist , it create a new file

FileMode.Create : Create a new file , if the file exist it will append to it

FileMode.CreateNew : Create a new File , if the file exist , it throws exception

FileMode.Open : Open an existing file

How to create a file using C# FileStream Class?

The following C# example shows , how to create and write in a file using FileStream.

         C# Source Code Download           Print Source Code
         How to use C# FileStream Class - Download
        
C# Tutorial

using System;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.IO.FileStream wFile;
                byte[] byteData = null;
                byteData = Encoding.ASCII.GetBytes("FileStream Test");
                wFile = new FileStream("c:\\streamtest.txt", FileMode.Append);
                wFile.Write(byteData, 0, byteData.Length);
                wFile.Close();
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.ToString());
           }
        }
    }
}

When we execute the above C# source code , it create a new File and write the content to the specified path .

CSharp File (I/O) Operations Related Contents
*     How to use C# Directory Class
*     How to use C# File Class
*     How to use C# Textreader Class
*     A simple C# Text Reader source code
*     How to use C# TextWriter Class
*     How to use C# BinaryWriter Class
*     How to use C# BinaryReader Class


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.