⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textinput.cs

📁 SmartNotePad v1.0原代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace File_browser
{
	/// <summary>
	/// rename 的摘要说明。
	/// </summary>
	public class textinput : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MenuItem menuSure;
		private System.Windows.Forms.MenuItem menuCancel;
		private System.Windows.Forms.Label title;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label dir;
		private System.Windows.Forms.TextBox newname;
		private System.Windows.Forms.MainMenu mainRename;
		private int atype;
		private string sfile;
	
		public textinput(string path,string name,int type)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			dir.Text = path;
			newname.Text = name;
			//新建目录
			if(type==0)
			{
				this.Text = "请输入新的目录名";
				title.Text = "请输入新的目录名";
			}
				//文件重命名
			else if(type==1)
			{
				this.Text = "请输入新的文件名";
				title.Text = "请输入新的文件名";
			}
				//目录重命名
			else if(type==2)
			{
				this.Text = "请输入新的目录名";
				title.Text = "请输入新的目录名";
			}
			atype = type;
			sfile = name;
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.mainRename = new System.Windows.Forms.MainMenu();
			this.menuSure = new System.Windows.Forms.MenuItem();
			this.menuCancel = new System.Windows.Forms.MenuItem();
			this.title = new System.Windows.Forms.Label();
			this.newname = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.dir = new System.Windows.Forms.Label();
			// 
			// mainRename
			// 
			this.mainRename.MenuItems.Add(this.menuSure);
			this.mainRename.MenuItems.Add(this.menuCancel);
			// 
			// menuSure
			// 
			this.menuSure.Text = "确定";
			this.menuSure.Click += new System.EventHandler(this.menuSure_Click);
			// 
			// menuCancel
			// 
			this.menuCancel.Text = "取消";
			this.menuCancel.Click += new System.EventHandler(this.menuCancel_Click);
			// 
			// title
			// 
			this.title.Location = new System.Drawing.Point(0, 112);
			this.title.Size = new System.Drawing.Size(168, 22);
			// 
			// newname
			// 
			this.newname.Location = new System.Drawing.Point(0, 144);
			this.newname.Size = new System.Drawing.Size(176, 25);
			this.newname.Text = "";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Nina", 9.75F, System.Drawing.FontStyle.Bold);
			this.label1.Location = new System.Drawing.Point(0, 8);
			this.label1.Size = new System.Drawing.Size(176, 22);
			this.label1.Text = "当前目录:";
			// 
			// dir
			// 
			this.dir.Font = new System.Drawing.Font("Nina", 9.75F, System.Drawing.FontStyle.Regular);
			this.dir.Location = new System.Drawing.Point(0, 32);
			this.dir.Size = new System.Drawing.Size(176, 22);
			// 
			// textinput
			// 
			this.Controls.Add(this.dir);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.newname);
			this.Controls.Add(this.title);
			this.Menu = this.mainRename;
			this.Text = "rename";

		}
		#endregion

		private void menuCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void menuSure_Click(object sender, System.EventArgs e)
		{
			if(NameCheck(newname.Text))
			{
				if(atype==0)
				{
					if(!Directory.Exists(dir.Text+newname.Text+"\\"))
					{
						Directory.CreateDirectory(dir.Text+newname.Text+"\\");
						this.Close();
					}
					else
						MessageBox.Show("此目录已存在,请换个目录名!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2);

				}
					//文件重命名
				else if(atype==1)
				{
					if(!File.Exists(dir.Text+newname.Text))
					{
						if(sfile.Substring(sfile.LastIndexOf(".")+1,3)!=newname.Text.Substring(newname.Text.LastIndexOf(".")+1,3))
						{
							if(MessageBox.Show("更改文件后缀可能会使该文件不能打开,您确定要这样?", this.Text,MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2).ToString()=="Yes")
							{
								File.Copy(dir.Text+sfile,dir.Text+newname.Text,true);
								File.Delete(dir.Text+sfile);
								this.Close();
							}
						}
						else
						{
							File.Copy(dir.Text+sfile,dir.Text+newname.Text,true);
							File.Delete(dir.Text+sfile);
							this.Close();
						}
					}
					else
						MessageBox.Show("此文件已存在,请换个文件名!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2);
				}
					//目录重命名
				else if(atype==2)
				{
					if(!Directory.Exists(dir.Text+newname.Text+"\\"))
					{
						Directory.Move(dir.Text+sfile,dir.Text+newname.Text);
						this.Close();
					}
					else
						MessageBox.Show("此目录已存在,请换个目录名!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2);
				}
			}
			else
				MessageBox.Show("名字中含有非法字符,请换个名字!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2);
		}

		//非法字符判定
		private bool NameCheck(string filename)
		{
			bool ifok = true;
			if(filename.IndexOf("?")>-1)
				ifok = false;
			if(filename.IndexOf("\\")>-1)
				ifok = false;
			if(filename.IndexOf("/")>-1)
				ifok = false;
			if(filename.IndexOf(":")>-1)
				ifok = false;
			if(filename.IndexOf("*")>-1)
				ifok = false;
			if(filename.IndexOf(">")>-1)
				ifok = false;
			if(filename.IndexOf("<")>-1)
				ifok = false;
			if(filename.IndexOf("|")>-1)
				ifok = false;
			if(filename.IndexOf("\"")>-1)
				ifok = false;
			return ifok;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -