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 txtWrite
{
public partial class Form1 : Form
{
private string currentFilePath;//存储打开文件的字段
public Form1()
{
InitializeComponent();
}
//打开文件
private void tspOpen_Click(object sender, EventArgs e)
{
if (ofdFile.ShowDialog() == DialogResult.OK)
{
getFileName(ofdFile.FileName);
txtFile.Text = File.ReadAllText(currentFilePath);
}
}
//保存
private void tsmSave_Click(object sender, EventArgs e)
{
File.WriteAllText(currentFilePath, txtFile.Text);
}
//另存为
private void tsmSaveas_Click(object sender, EventArgs e)
{
if (sfdSave.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfdSave.FileName, txtFile.Text);
getFileName(sfdSave.FileName);
}
}
private string getFileName(string filePath)
{
currentFilePath = filePath;
string filename = Path.GetFileName(currentFilePath);//从一个完整路径中取出文件名
this.Text = filename + "-简单的记事本";
return currentFilePath;
}
private void tsmExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//复制
private void cOPYToolStripMenuItem_Click(object sender, EventArgs e)
{
txtFile.Copy();
}
//粘贴
private void tsmPaste_Click(object sender, EventArgs e)
{
txtFile.Paste();
}
//剪切
private void tsmCut_Click(object sender, EventArgs e)
{
txtFile.Cut();
}
}
}
本人相当的菜鸟,高手见了本文莫笑!
刚刚学c#不久