CSharp.Net-Informations.com

  How to use C# File Class




   Categories

    HOME
    VB.NET
    CSHARP


   














How to use C# File Class

File class is using for the File operations in C#. We can create , delete , copy etc. operations do with C# File class.

How to create a File using C# File class ?

In order to create a new File using C# File class , we can call Create method in the File class.

  Syntax : FileSttream File.Create(string FilePath)
  FilePath : The name of the new File Object

CSharp Code : File.Create("c:\\testFile.txt");

How to check a File exist or not using C# File class ?

Before we creating a File object , we usually check that File exist or not. For that we are using the Exists method in the C# File class.

  Syntax : bool File.Exists(string FilePath)
  FilePath : The name of the File
  bool : Returns true or false -
  if File exist it Returns true else Returns false

CSharp Code : File.Exists("c:\\testFile.txt")

The following C# source code shows these operations :


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

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

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

        private void button1_Click(object sender, EventArgs e)
        {
			if (File.Exists("c:\\testFile.txt"))
			{
				//shows message if testFile exist 
				MessageBox.Show ("File 'testFile' Exist ");
			}
			else
			{
				//create the file testFile.txt 
				File.Create("c:\\testFile.txt");
				MessageBox.Show("File 'testFile' created ");
			}
        }
    }
}

		

CSharp File (I/O) Operations Related Contents
*     How to use C# Directory Class
*     How to use C# FileStream Class
*     How to use C# Textreader Class
*     How to use C# Textreader Class
*     How to use C# TextWriter Class
*     How to use C# BinaryWriter Class
*     How to use C# BinaryReader Class

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
Search here for more CSharp Source Code :

  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2010 All Rights Reserved