//讀文件
private void readBtn_Click(object sender, EventArgs e)
{
try
{
if (pathTxt.Text == "")
{
MessageBox.Show("請輸入文件地址");
return;
}
readTxt.Text = File.ReadAllText(pathTxt.Text, Encoding.Default);//不需要對地址中的"\"進行轉義
}
catch (Exception ex)
{
MessageBox.Show("文件地址錯誤");
}
}
//寫文件
private void writeBtn_Click(object sender, EventArgs e)
{
try
{
if (pathTxt.Text == "")
{
MessageBox.Show("請輸入文件地址");
return;
}
File.WriteAllText(pathTxt.Text, writeTxt.Text);
MessageBox.Show("success");
}
catch (Exception ex)
{
MessageBox.Show("文件地址錯誤");
}
}
//讀取文件夾路徑
private void folderBtn_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
folderTxt.Text = folderBrowserDialog1.SelectedPath;
}
}
//讀取文件路徑
private void fileBtn_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "Text files (*.txt)|*.txt|All Files(*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
pathTxt.Text = op.FileName;
}
}