?? saveas.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace notepad
{
/// <summary>
/// Saveas 的摘要說明。
/// </summary>
public class Saveas : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainSaveas;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox filename;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.MenuItem menuItem2;
private string path = "\\",acontent;
public Saveas(string apath,string afilename,string content)
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
this.Text = "請選擇另存的目錄";
filename.Text = afilename;
if(apath!="")
path = apath;
acontent = content;
listView1.SmallImageList = this.imageList1;
listView1.SmallImageList.ImageSize = new Size(16, 16);
GetList();
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Saveas));
this.mainSaveas = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.listView1 = new System.Windows.Forms.ListView();
this.label1 = new System.Windows.Forms.Label();
this.filename = new System.Windows.Forms.TextBox();
this.imageList1 = new System.Windows.Forms.ImageList();
//
// mainSaveas
//
this.mainSaveas.MenuItems.Add(this.menuItem1);
this.mainSaveas.MenuItems.Add(this.menuItem2);
//
// menuItem1
//
this.menuItem1.Text = "保存";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Text = "取消";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// listView1
//
this.listView1.FullRowSelect = true;
this.listView1.Size = new System.Drawing.Size(176, 128);
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋體", 9.75F, System.Drawing.FontStyle.Regular);
this.label1.Location = new System.Drawing.Point(0, 136);
this.label1.Size = new System.Drawing.Size(152, 16);
this.label1.Text = "文件名:";
//
// filename
//
this.filename.Font = new System.Drawing.Font("宋體", 9.75F, System.Drawing.FontStyle.Regular);
this.filename.Location = new System.Drawing.Point(0, 152);
this.filename.Size = new System.Drawing.Size(176, 22);
this.filename.Text = "";
//
// imageList1
//
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
//
// Saveas
//
this.Controls.Add(this.filename);
this.Controls.Add(this.label1);
this.Controls.Add(this.listView1);
this.Menu = this.mainSaveas;
this.Text = "Saveas";
}
#endregion
private void GetList()
{
listView1.Clear();
listView1.Columns.Add(path, listView1.Size.Width, HorizontalAlignment.Left);
// Add the list view items
DirectoryInfo dir = new DirectoryInfo(path);
if(path!="\\")
{
ListViewItem item = new ListViewItem("..");
item.ImageIndex = 0;
listView1.Items.Add(item);
}
foreach (DirectoryInfo f in dir.GetDirectories())
{
String name = f.Name;
ListViewItem item = new ListViewItem(name);
item.ImageIndex = 0;
listView1.Items.Add(item);
}
foreach (FileInfo f in dir.GetFiles("*.txt"))
{
String name = f.Name;
ListViewItem item = new ListViewItem(name);
item.ImageIndex = 1;
listView1.Items.Add(item);
}
}
private void DirDecide()
{
if(listView1.FocusedItem!=null)
{
string newstr = listView1.FocusedItem.Text;
if(newstr=="..")
{
path = path.Substring(0,(path.LastIndexOf("\\")));
path = path.Substring(0,path.LastIndexOf("\\")+1);
GetList();
}
else if(newstr.IndexOf(".txt")==-1&&newstr.IndexOf(".TXT")==-1)
{
path += newstr+"\\";
GetList();
}
else
{
filename.Text = newstr;
}
filename.Focus();
}
}
private void listView1_ItemActivate(object sender, System.EventArgs e)
{
DirDecide();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
if (File.Exists(path+filename.Text))
{
if(MessageBox.Show("當前文件已存在,要覆蓋嗎?", this.Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2).ToString()=="Yes")
{
File.Delete(path+filename.Text);
FileWrite(path+filename.Text);
}
}
else
FileWrite(path+filename.Text);
Start start = new Start(path,filename.Text);
start.Show();
this.Close();
}
private void FileWrite(string filepath)
{
//創建新文件并寫入
using (StreamWriter fs = new StreamWriter(filepath,false,System.Text.Encoding.Default))
{
string[] content = System.Text.RegularExpressions.Regex.Split(acontent,"\r\n");
for(int i=0;i<content.Length;i++)
fs.WriteLine(content[i]);
fs.Close();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -