using System;
using System.Windows.Forms;
using System.IO;
namespace InputOutput
{
public partial class MainForm : Form
{
//We'll use this path later on to hold the current path of the file
public string ourPath;
public MainForm()
{
InitializeComponent();
}
void CmdBrowseClick(object sender, EventArgs e)
{
//Create a OpenFileDialog instance to use
OpenFileDialog openDialog = new OpenFileDialog();
//We select what types are permitted to open
openDialog.Filter = "Text Files (*.txt)|*.txt";
//A title for the dialogbox
openDialog.Title = "Open textfile";
//If the user press cancel then end this void
if (openDialog.ShowDialog() == DialogResult.Cancel) {
return;
}
//Create a filestream
FileStream fStr;
try {
//Set filestream to the result of the pick of the user
fStr = new FileStream(openDialog.FileName, FileMode.Open, FileAccess.Read);
//Create a streamreader, sr, to read the file
StreamReader sr = new StreamReader(fStr);
//While the end of the file has not been reached...
while (sr.Peek() >= 0) {
//Create a 'line' that contains the current line of the textfile
string line = sr.ReadLine();
//Enter the text into the main textbox together with a new line
txtMain.Text += line + Environment.NewLine;
}
//Close the file so other modules can access it
sr.Close();
//Set public variable ourPath to the current path
ourPath = Path.GetDirectoryName(openDialog.FileName);
ourPath += Path.GetFileName(openDialog.FileName);
//If something goes wrong, tell the user
} catch (Exception) {
MessageBox.Show("Error opening file", "Error message");
}
}
void CmdAlterFileClick(object sender, EventArgs e)
{
//Open a streamwriter using public string ourPath
StreamWriter sw = new StreamWriter(ourPath);
//Write the textbox values into the textfile
sw.Write(txtMain.Text);
//Close the textfile
sw.Close();
}
}
}
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read);
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Write);
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Append);
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Append);
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.CreateNew);
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only