On file created or deleted

   



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

class MainClass {
    static void Main(string[] args) {
        using (FileSystemWatcher watch = new FileSystemWatcher()) {
            watch.Path = Application.StartupPath;
            watch.Filter = "*.*";
            watch.IncludeSubdirectories = true;

            // Attach the event handler.
            watch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
            watch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
            watch.EnableRaisingEvents = true;

            Console.WriteLine("Press Enter to create a  file.");
            Console.ReadLine();

            if (File.Exists("test.bin")) {
                File.Delete("test.bin");
            }

            // Create test.bin.
            using (FileStream fs = new FileStream("test.bin", FileMode.Create)) {
                // Do something.
            }

            Console.WriteLine("Press Enter to terminate the application.");
            Console.ReadLine();
        }
    }

    private static void OnCreatedOrDeleted(object sender, FileSystemEventArgs e) {
        Console.WriteLine("	NOTIFICATION: " + e.FullPath + "' was " + e.ChangeType.ToString());
        Console.WriteLine();
    }
}
           
          


Set NotifyFilter of FileSystemWatcher

   


using System;
using System.IO;

public class Program {
    static void Main(string[] args) {
        FileSystemWatcher watcher = new FileSystemWatcher();
        try {
            watcher.Path = @"C:MyFolder";
        } catch (ArgumentException ex) {
            Console.WriteLine(ex.Message);
            return;
        }

        watcher.NotifyFilter = NotifyFilters.LastAccess
            | NotifyFilters.LastWrite
            | NotifyFilters.FileName
            | NotifyFilters.DirectoryName;

        watcher.Filter = "*.txt";

        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);
        watcher.EnableRaisingEvents = true;

        Console.WriteLine(@"Press 'q' to quit app.");
        while (Console.Read() != 'q') ;
    }
    private static void OnChanged(object source, FileSystemEventArgs e) {
        Console.WriteLine("File: {0} {1}!", e.FullPath, e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e) {
        Console.WriteLine("File: {0} renamed to
{1}", e.OldFullPath, e.FullPath);
    }
}


           
          


Write byte array to a file

   
 


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class Program {
    static void Main(string[] args) {
        byte[] byData;
        char[] charData;

        try {
            FileStream aFile = new FileStream("Temp.txt", FileMode.Create);
            charData = "www.kutayzorlu.com/java2s/com".ToCharArray();
            byData = new byte[charData.Length];
            Encoder e = Encoding.UTF8.GetEncoder();
            e.GetBytes(charData, 0, charData.Length, byData, 0, true);

            aFile.Seek(0, SeekOrigin.Begin);
            aFile.Write(byData, 0, byData.Length);
        } catch (IOException ex) {
            Console.WriteLine(ex.ToString());
            return;
        }
    }
}

    


Seek from Begin

   
 


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;



class Program {
    static void Main(string[] args) {
        byte[] byData = new byte[200];
        char[] charData = new Char[200];

        try {
            FileStream aFile = new FileStream("Program.cs", FileMode.Open);
            aFile.Seek(135, SeekOrigin.Begin);
            aFile.Read(byData, 0, 200);
        } catch (IOException e) {
            Console.WriteLine("An IO exception has been thrown!");
            Console.WriteLine(e.ToString());
            Console.ReadKey();
            return;
        }

        Decoder d = Encoding.UTF8.GetDecoder();
        d.GetChars(byData, 0, byData.Length, charData, 0);

        Console.WriteLine(charData);
    }
}

    


Using FileStreams

/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;

namespace Client.Chapter_11___File_and_Streams
{
public class UsingFileStreams {
static void Main(string[] args)
{
//Creates a file with read-write access that allows others to read
FileStream MyFileStream1 = new FileStream(@”c:ProjectsTesting.txt”, FileMode.Create);
FileInfo MyFiles = new FileInfo(@”c:ProjectsTesting.txt”);
FileStream MyFileStream2 = MyFiles.OpenRead();

//or any of the following
MyFileStream2 = MyFiles.OpenWrite();
MyFileStream2 = MyFiles.Open(FileMode.Append, FileAccess.Read, FileShare.None);
MyFileStream2 = MyFiles.Create();

//You can read file stream ona pe byte basis or as an array of bytes
int MyBytes = MyFileStream1.ReadByte();

//or
int NumberOfBytes = 200;
byte[] MyByteArray = new Byte[NumberOfBytes];
int BytesRead = MyFileStream1.Read(MyByteArray, 0, NumberOfBytes);

//Data can be written to FileStreams as well through bytes or arrays of //bytes
byte MyWriteByte = 100;

MyFileStream1.WriteByte(MyWriteByte);

//or via an array
int NumberOfBytesToWrite = 256;
byte[] MyWriteByteArray = new Byte[NumberOfBytesToWrite];

for (int i = 0; i < 256; i++) { MyWriteByteArray[i] = (byte)i; i++; } MyFileStream1.Write(MyWriteByteArray, 0, NumberOfBytesToWrite); MyFileStream1.Close(); MyFileStream2.Close(); } } } [/csharp]