Monday, May 31, 2010

Retrieve Information About a File

Following example takes a file path from textbox named by txtpath, and the display the information about the file.

User Interface













Coding Part
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileUpload
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            string fileName = "";
            string path = "";
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Attach customer proposal document";
            fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
            fDialog.InitialDirectory = @"C:\";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = System.IO.Path.GetFileName(fDialog.FileName);
                path = Path.GetDirectoryName(fDialog.FileName);
                txtpath.Text = path + "\\" + fileName;

            }
        }

        private void btnShowInfo_Click(object sender, EventArgs e)
        {
            //To display file information
            FileInfo myFile = new FileInfo(txtpath.Text);

            rtxtInfor.Text = "Checking File: " + myFile.Name+"\n";
            rtxtInfor.Text=rtxtInfor.Text+"File exists: "+myFile.Exists.ToString()+"\n";

            if (myFile.Exists)
            {
                rtxtInfor.Text = rtxtInfor.Text + "File Created:" + myFile.CreationTime.ToString() + "\n";
                rtxtInfor.Text = rtxtInfor.Text + "File Last Updated: " + myFile.LastWriteTime.ToString() + "\n";
                rtxtInfor.Text = rtxtInfor.Text + "File Last accessed: " + myFile.LastAccessTime.ToString() + "\n";
                rtxtInfor.Text = rtxtInfor.Text + "File Size (bytes): " + myFile.Length.ToString() +"\n";
                rtxtInfor.Text = rtxtInfor.Text + "File attribute list: " + myFile.Attributes.ToString() + "\n";

            }




        }
    }
}














No comments: