Csharp/CSharp Tutorial/File Directory Stream/File Create

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

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

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;
    }
}

Create text file using File.CreateText

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();
    }
  }
}

Creating Files

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