Csharp/CSharp Tutorial/File Directory Stream/File Create — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:20, 26 мая 2010

Create a file and get its creation time, full name and Attributes

<source lang="csharp">using System; using System.IO; public class MainClass {

   public static int Main(string[] args)
   {      
   FileInfo f = new FileInfo(@"C:\Test.txt");
   FileStream fs = f.Create();
   Console.WriteLine("Creation: {0}", f.CreationTime);
   Console.WriteLine("Full name: {0}", f.FullName);
   Console.WriteLine("Full atts: {0}", f.Attributes.ToString());
   fs.Close();
   f.Delete();      
       return 0;
   }

}</source>

Create text file using File.CreateText

<source lang="csharp">using System; using System.IO; class MainClass {

 static void Main(string[] args)
 {
   StreamWriter MyStream = null;
   string MyString = "Hello World";
   try
   {
     MyStream = File.CreateText("MyFile.txt");
     MyStream.Write(MyString);
   }
   catch (IOException e)
   {
     Console.WriteLine(e);
   }
   catch (Exception e)
   {
     Console.WriteLine(e);
   }
   finally
   {
     if (MyStream != null)
       MyStream.Close();
   }
 }

}</source>

Creating Files

<source lang="csharp">using System; using System.IO; public class MainClass {

 public static void Main()
 {
   FileInfo MyFile = new FileInfo(@"c:\Test.txt");
   MyFile.Create();
 }

}</source>