using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wfa0022
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
FileInfo fi = new FileInfo(textBox1.Text);
FileStream fs = fi.Create();
MessageBox.Show("File Has been Created");
//Console.WriteLine("File Has been Created");
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo fi = new FileInfo(textBox1.Text);
StreamWriter str = fi.CreateText();
str.WriteLine(textBox3.Text);
MessageBox.Show("File has been Created with text");
//Console.WriteLine("File has been Created with text");
str.Close();
}
=====================================
What is FileInfo?
FileInfo used for typical operations such as copying, moving, renaming, creating, opening, deleting and appending to files.
It uses StreamWriter class to write data and StreamReader class to read data to the file.
It is sealed class so we cannot be inherited.
FileInfo class Provides Methods and Properties.
FileInfo Parent class and namespace
FileInfo class is a System.IO namespace.
FileInfo’s Parent class is a FileSystemInfo.
FileInfo Properties
Attributes
CreationTime
CreationTime Utc
Directory
DirectoryName
Exists
Extension
FullName
IsReadOnly
LastAccessTime
LastAccessTimeUtc
LastWriteTime
LastWriteTimeUtc
Length
Name
FileInfo Methods
AppendText()
CopyTo()
Create()
CreateText()
Decrypt()
Delete()
Encrypt()
GetAccessControl()
GetLifetimeServices()
Replace()
InitializeLifetimeService()
MemberWiseClone()
MoveTo()
Open()
OpenRead()
OpenText()
OpenWrite()
Refresh()
Code for FileInfo class demo :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileInfoDemo
{
class Program
{
static void Main(string[] args)
{
//Create Method
FileInfo fi = new FileInfo(@"E:/file/MyTestFile.txt");
FileStream fs = fi.Create();
Console.WriteLine("File Has been Created");
//CreateText Method
FileInfo fi = new FileInfo(@"E:/file/MyTestFile.txt");
StreamWriter str = fi.CreateText();
str.WriteLine("Ankpro");
Console.WriteLine("File has been Created with text");
str.Close();
//Delete Method
FileInfo fi = new FileInfo(@"E:/file/MyTestFile.txt");
fi.Delete();
Console.WriteLine("File has been deleted");
//copy method
string path = @"E:/file/MyTestFile.txt";
string path2 = @"E:/copy/MyTestFile.txt";
FileInfo f1 = new FileInfo(path);
FileInfo f2 = new FileInfo(path2);
f1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}." , path, path2);
// move method
string path = @"E:/file/Move.txt";
string path2 = @"E:/copy/Move.txt";
FileInfo f1 = new FileInfo(path);
FileInfo f2 = new FileInfo(path2);
f1.MoveTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//opentext method
FileInfo fi = new FileInfo(@"E:/file/MyTestFile.txt");
StreamReader sr = fi.OpenText();
string s = "";
while((s=sr.ReadLine())!=null)
{
Console.WriteLine(s);
}
FileInfo fi = new FileInfo(@"E:/file/MyTestFile.txt");
Console.WriteLine("File name is {0}", fi.Name);
Console.WriteLine("File Creation Time is {0}", fi.CreationTime);
Console.WriteLine("File LastAccess Time is {0}", fi.LastAccessTime);
Console.WriteLine("File Length is {0}", fi.Length +"bytes");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//Delete Method
FileInfo fi = new FileInfo(textBox1.Text);
fi.Delete();
MessageBox.Show("File has been deleted");
//Console.WriteLine("File has been deleted");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
//copy method
string path = textBox1.Text;
string path2 = textBox2.Text;
FileInfo f1 = new FileInfo(path);
FileInfo f2 = new FileInfo(path2);
f1.CopyTo(path2);
MessageBox.Show(path+" was copied to "+ path2);
//Console.WriteLine("{0} was copied to {1}.", path, path2);
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button6_Click(object sender, EventArgs e)
{
//Move method
string path = textBox1.Text;
string path2 = textBox2.Text;
FileInfo f1 = new FileInfo(path);
FileInfo f2 = new FileInfo(path2);
f1.MoveTo(path2);
MessageBox.Show(path + " was moved to " + path2);
}
private void button7_Click(object sender, EventArgs e)
{
string path = textBox1.Text;
StreamReader sr = new StreamReader(path);
string rAlllines = sr.ReadToEnd();
textBox4.Text = rAlllines;
sr.Close();
}
private void button8_Click(object sender, EventArgs e)
{
FileInfo fi = new FileInfo(textBox1.Text);
StreamReader sr = fi.OpenText();
textBox4.Text = "";
string s = "";
while ((s = sr.ReadLine()) != null)
{
textBox4.Text += s + " " + "\r\n";
}
}
private void button9_Click(object sender, EventArgs e)
{
FileInfo fi = new FileInfo(textBox1.Text);
MessageBox.Show("File name is "+ fi.Name);
MessageBox.Show("File Creation Time is "+ fi.CreationTime);
MessageBox.Show("File LastAccess Time is "+fi.LastAccessTime);
MessageBox.Show("File Length is "+ fi.Length + "bytes");
}
}
}