Create Windows Form Application to Get File Information in Asp.Net



 In this article we will create windows form application to read files and get information from that file from creation to modified, date time, name etc.
Here is the Design to Design your Windows form.

DESIGN :


Now Code for Browse File from Directory and get File Information and Display in Windows Form like below. Here I have set to directly open C drive for browse file you can set your drive to open default path.

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "All files (*.*)|*.*";
            openFileDialog1.Title = "Select File";

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    txtFilePath.Text = openFileDialog1.FileName.ToString();
                    ShowFileInfo(txtFilePath.Text.Trim());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk: " + ex.Message.ToString());
                }
            }
        }
Now Create “ShowFileInfo” Method and Retrieve Information from Browsed file.

private void ShowFileInfo(string sFilePath)
        {
            // Part 1:  File Version Information
            System.Diagnostics.FileVersionInfo fileVersInfo =
                System.Diagnostics.FileVersionInfo.GetVersionInfo(sFilePath);

            lblCompanyName.Text = "Company Name:  " + fileVersInfo.CompanyName;
            lblFileName.Text = "File Name:  " + fileVersInfo.FileName;
            lblProductName.Text = "Product Name:  " + fileVersInfo.ProductName;
            lblVersion.Text = "Version:  " + fileVersInfo.FileVersion;
            lblComments.Text = "Comments:  " + fileVersInfo.Comments;
            lblIsPatched.Text = "Is Patched:  " + fileVersInfo.IsPatched;
            lblCopyright.Text = "Copyright:  " + fileVersInfo.LegalCopyright;
            lblTrademark.Text = "Trademark:  " + fileVersInfo.LegalTrademarks;
            lblDescription.Text = "Description: " + fileVersInfo.FileDescription;
            lblInternalName.Text = "Internal Name:  " + fileVersInfo.InternalName;

            // Part 2:  File Information
            System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath);

            lblAttributes.Text = "Attributes:  " + fi.Attributes.ToString();
            lblCreationDate.Text = "Creation Date:  " + fi.CreationTime.ToLongDateString();
            lblCreationTime.Text = "Creation Time:  " + fi.CreationTime.ToLongTimeString();
            lblLastAccessDate.Text = "Last Access Date: " + fi.LastAccessTime.ToLongDateString();
            lblLastAccessTime.Text = "Last Access Time:  " + fi.LastAccessTime.ToLongTimeString();
            lblLastWriteDate.Text = "Last Write Date:" + fi.LastWriteTime.ToLongDateString();
            lblLastWriteTime.Text = "Last Write Time:" + fi.LastWriteTime.ToLongTimeString();
            lblFileSize.Text = "File Size:  " + fi.Length.ToString();
            lblIsReadOnly.Text = "Read Only:  " +  fi.IsReadOnly.ToString();

            // these values can all be changed
            //fi.LastAccessTime = DateTime.Now;
            //fi.CreationTime = DateTime.Now;
            //fi.LastWriteTime = DateTime.Now;
        }

Now you are done to Get File Information Now run your Application and Browse file from Directory and get information and Display it in Windows form

Related Posts

Previous
Next Post »

Thanks for comments.....