SIGN UP MEMBER LOGIN:    
ARTICLE

C# File Handling with Dialogs

Posted by Deepak Dwij Articles | C# Language November 14, 2011
File data needs to be processed in nearly every non-trivial program, and the classes in the base class library that you can use have lots of details, we evaluate file handling.Dialog boxes are used to gather input from users. You can create your own dialog boxes or use the built-in dialog boxes
Reader Level:
Download Files:
 

Introduction

File data needs to be processed in nearly every non-trivial program, and the classes in the base class library that you can use have lots of details. With these benchmarks and examples focused on file IO in the C# language, we evaluate file handling.

Dialog boxes are used to gather input from users. You can create your own dialog boxes or use the built-in dialog boxes.

Dialogs

  • OpenFileDialog
  • SaveFileDialog
  • FolderBrowserDialog
  • FontDialog
  • ColorDialog

Design

In the design form section ,drag 'n drop dialogs, a richtextbox and five button control as show below:

1.gif

Program

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace filehanding
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
              StreamReader sr = new StreamReader(openFileDialog1.FileName);
              richTextBox1.Text=sr.ReadToEnd();
              sr.Close();
            }
       }       
        private void button3_Click(object sender, EventArgs e)
        {
            FontDialog font = new FontDialog();
            if (font.ShowDialog() != DialogResult.Cancel)
            {
               richTextBox1.Font = font.Font;           
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }
        private void button2_Click_1(object sender, EventArgs e)
        {
            //saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
            saveFileDialog1.Filter = "txt files (*.doc)|*.doc";
           if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFileDialog1.FileName.Length > 0)
            {
                 richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Text = folderBrowserDialog1.SelectedPath;
            }
 
        }
    }
}

Output: To Run hit F5.

Open file Button


2.gif

Result

3.gif

Save file Button

4.gif 

Browse folder Button

browse-1.gif

Result


r-browse.gif

Font Change Button

5.gif

Result


6.gif

Change Color Button

Untitled-7.gif

Result


8.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor