C#
  Home arrow C# arrow Page 6 - C# Simplified, part 5: Error Handling and ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Simplified, part 5: Error Handling and Files
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 15
    2005-05-31

    Table of Contents:
  • C# Simplified, part 5: Error Handling and Files
  • Try-Catch clause
  • Finally clause
  • Accessing files and directories
  • Displaying all files under a directory
  • Creating files
  • Reading from a file
  • Copying a file

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C# Simplified, part 5: Error Handling and Files - Creating files


    (Page 6 of 8 )

    With C#, you can create new files with the help of the Create() method of the FileInfo class. You can also access specific information about the file. You can even delete a file. In listing 5.8, a file named Mycsharp.txt is created on the D drive of your system. After creating the file, the program displays some useful properties.

    Listing 5.8

    using System;

    using System.IO;

          class CreateFile

          {

                public static void Main()

                {

                  

                   FileInfo finfo = new FileInfo(@"D:\Mycsharp.txt");

                   FileStream fstream = finfo.Create();

                   Console.WriteLine("File Mycsharp.txt created");

                   Console.WriteLine("Creation Time: {0}",finfo.CreationTime);

                   Console.WriteLine("Full Name: {0}",finfo.FullName);

                   Console.WriteLine("FileAttributes: {0}",finfo.Attributes.ToString());

                   fstream.Close();

                  

                }

          }

    You can verify the existence of the file by going to the D drive using My Computer or Windows Explorer.

    Listing 5.9 is a continuation of listing 5.8. After creating the file, the program will ask you to press any key to delete the file. If you press a key the file system will close, and the created file will be deleted from your system.

    Listing 5.9

    using System;

    using System.IO;

          class CreateFile

          {

                public static void Main()

                {

                  

                   FileInfo finfo = new FileInfo(@"D:\Mycsharp.txt");

                   FileStream fstream = finfo.Create();

                   Console.WriteLine("File Mycsharp.txt created");

                   Console.WriteLine("Creation Time: {0}",finfo.CreationTime);

                   Console.WriteLine("Full Name: {0}",finfo.FullName);

                   Console.WriteLine("FileAttributes: {0}",finfo.Attributes.ToString());

                   Console.WriteLine("Press any key to delete the file");

                   Console.Read();

                   fstream.Close();

                   finfo.Delete();

                   Console.WriteLine("File Mycsharp.txt deleted");

                }

          }

    More C# Articles
    More By Anand Narayanaswamy


     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway