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

📄 cfile.cs

📁 文件系统管理 在内存中为文件开辟存储空间 可以创建文件和文件夹 (至少有两级目录 :模拟DOS文件系统的操作 可以存储文件的内容 可以查看删除和修改文件(15 2
💻 CS
字号:
using System;
using System.IO;

namespace FSM
{
	/// <summary>
	/// 
	/// </summary>
	public class cFile
	{
		public cFile()
		{
			// 
			// TODO: 在此处添加构造函数逻辑
			//
		}
		public void Copy(string sourceFileName,string destFileName)
		{
			char[] spliter = new char[] {'\\'};
			if( sourceFileName.IndexOf(":") == 1 )//Source file name is full path
			{
				if( !File.Exists( sourceFileName ) )
				{
					Console.Write( "源文件'" + sourceFileName + "'不存在\n");		
					return;
				}
			}
			else//Source file name is opposite path
			{
				sourceFileName = Directory.GetCurrentDirectory() + @"\" + sourceName;
			}
			if( destFileName.IndexOf(":") == 1 )//Destination file name is full path
			{
				if( !Directory.Exists( Path.GetDirectoryName( destFileName)) )
				{
					Console.Write( "目标目录'" + Path.GetFullPath( destFileName) + "'不存在\n");		
					return;
				}
			}
			else//Destination file name is opposite path
			{
				destFileName = Path.GetDirectoryName(sourceFileName) + @"\" + destFileName;
			}
			if( File.Exists( destFileName ) )
			{
				Console.WriteLine( destFileName + "已存在,是否替换? Y/N " );
				if( Console.ReadLine().ToLower() == "y" )
				{
					File.Copy( sourceFileName,destFileName,true);
					if( File.Exists( destFileName ) )
					{
						Console.WriteLine( Path.GetFileName( sourceFileName ));
						return;
					}
					else
					{
						Console.WriteLine( "复制文件'" + Path.GetFileName( sourceFileName ) + "'失败");
					}
				}
				else return;
			}
			try
			{
				File.Copy( sourceFileName,destFileName );
			}
			catch( Exception err )
			{
				Console.Write( err.Message + "\n");		
				return;
			}
			if( File.Exists( destFileName ) )
			{
				Console.Write( Path.GetFileName( sourceFileName ) + "\n");
			}
			else
			{
				Console.WriteLine( "复制文件'" + Path.GetFileName( sourceFileName ) + "'失败");
			}
			
		}
		public void Create(string path)
		{
			FileStream fs;
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !Directory.Exists( Path.GetDirectoryName(path)) )
				{
					Console.Write( "目录'" + path + "'不存在\n");		
					return ;
				}
			}
			else//file name is opposite path
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( !File.Exists( path ) )
			{				
				try
				{
					fs = File.Create( path );
					fs.Close();
				}
				catch( Exception err )
				{
					Console.Write( err.Message + "\n");	
					return ;
				}
				if( !File.Exists( path ) )
				{
					Console.WriteLine( "创建文件'" + Path.GetFileName( path ) + "'失败");
					return ;
				}
				else
				{
					return;
				}
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'已存在\n");		
				return ;
			}
		}		
		public void Delete(string path)
		{
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !File.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}			
			else//file name is opposite path
				
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				Console.WriteLine( "确定删除文件? Y/N " );
				if( Console.ReadLine().ToLower() == "y" )
				{
					try
					{
						File.Delete( path );
					}
					catch( Exception err )
					{
						Console.Write( err.Message + "\n");		
						return;
					}
					if( File.Exists( path ) )
					{
						Console.WriteLine( "删除文件'" + Path.GetFileName( path ) + "'失败");
					}				
				}
				else return;				
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
		public void DeleteNoConfirm(string path)
		{
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !File.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}			
			else//file name is opposite path
				
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{				
				try
				{
					File.Delete( path );
				}
				catch( Exception err )
				{
					Console.Write( err.Message + "\n");		
					return;
				}
				if( File.Exists( path ) )
				{
					Console.WriteLine( "删除文件'" + Path.GetFileName( path ) + "'失败");
				}												
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
		public void Move(string sourceFileName,string destFileName)
		{
			char[] spliter = new char[] {'\\'};
			if( sourceFileName.IndexOf(":") == 1 )//Source file name is full path
			{
				if( !File.Exists( sourceFileName ) )
				{
					Console.Write( "源文件'" + sourceFileName + "'不存在\n");		
					return;
				}
			}
			else//Source file name is opposite path
			{
				sourceFileName = Directory.GetCurrentDirectory() + @"\" + sourceFileName;
			}
			if( destFileName.IndexOf(":") == 1 )//Destination file name is full path
			{
				if( !Directory.Exists( Path.GetDirectoryName( destFileName)) )
				{
					Console.Write( "目标目录'" + Path.GetFullPath( destFileName) + "'不存在\n");		
					return;
				}
			}
			else//Destination file name is opposite path
			{
				destFileName = Path.GetDirectoryName(sourceFileName) + @"\" + destFileName;
			}
			if( !File.Exists( sourceFileName ) )
			{
				Console.WriteLine( destFileName + "不存在" );				
				return;
			}
			if( File.Exists( destFileName ) )
			{
				Console.WriteLine( destFileName + "已存在" );	
				return;
			}			
			try
			{
				File.Move( sourceFileName,destFileName );
			}
			catch( Exception err )
			{
				Console.Write( err.Message + "\n");		
				return;
			}
			if( !File.Exists( destFileName ) )
			{
				Console.WriteLine( "移动文件'" + Path.GetFileName( sourceFileName ) + "'失败");
			}
		
		}
		public void Edit( string path )
		{
			StreamReader sr;
			StreamWriter wr;
			string line;

			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !Directory.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}
			else//file name is opposite path
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				sr = File.OpenText( path );//加异常处理
				Console.WriteLine( "文件内容如下:\n" );
				while( true )
				{
					line = sr.ReadLine();
					if( line != null )
					{
						Console.WriteLine( line );
					}
					else
					{
						break;
					}
				}
				sr.Close();
				Console.WriteLine( "\n请输入文件的新内容(以'@'结束输入):\n" );
				wr = new StreamWriter(path,false,System.Text.Encoding.UTF8);
                while( true )
				{	
					line = Console.ReadLine();
					if( line.IndexOf("@") == -1 )
					{
						wr.WriteLine( line );
					}
					else
					{
						break;
					}
				}
				wr.Close();				
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
		public void Type( string path )
		{
			StreamReader sr;
			string line;

			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !Directory.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}
			else//file name is opposite path
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				sr = File.OpenText( path );
				Console.WriteLine( "文件内容如下:\n" );
				while( true )
				{
					line = sr.ReadLine();
					if( line != null )
					{
						Console.WriteLine( line );
					}
					else
					{
						break;
					}
				}
				sr.Close();
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
		public void ReName( string path , string NewName)
		{
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !Directory.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}
			else//file name is opposite path
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				NewName = Path.GetDirectoryName(path) + @"\" + NewName;
				if( !File.Exists( NewName ) )
				{
					File.Move( path,NewName );
				}
				else
				{
					Console.Write( "文件'" + NewName + "'已存在\n");		
					return;
				}
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
		public void SetAttribute( string path,string[] attributeArray)
		{	
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !Directory.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}
			else//file name is opposite path
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				foreach( string attr in attributeArray )
				{
					
					if( attr == "+h")
					{
						if ((File.GetAttributes(path) & FileAttributes.Hidden) != FileAttributes.Hidden)//Not hidden
						{
							// Hide the file.
							File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);								
						}
					}
					if( attr == "+r")
					{
						if ((File.GetAttributes(path) 
							& FileAttributes.ReadOnly) != FileAttributes.ReadOnly)//Not readonly
						{
							// Readonly the file.
							File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);								
						} 
					}
					if( attr == "+s")
					{
						if ((File.GetAttributes(path) 
							& FileAttributes.ReadOnly) != FileAttributes.System)//Not system
						{
							// System the file.
							File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.System);								
						} 
					}
					if( attr == "-h")
					{
						if ((File.GetAttributes(path) 
							& FileAttributes.Hidden) == FileAttributes.Hidden)//File is hidden
						{
							// Show the file.
							FileAttributes fa = (File.GetAttributes(path) ^ FileAttributes.Hidden);
							File.SetAttributes(path, fa );
						}
					}
					if( attr == "-r")
					{
						if ((File.GetAttributes(path) 
							& FileAttributes.ReadOnly) == FileAttributes.ReadOnly)//File is readonly
						{
							// Writable the file.
							File.SetAttributes(path, (File.GetAttributes(path) ^ FileAttributes.ReadOnly));
						}
					}
					if( attr == "-s")
					{
						if ((File.GetAttributes(path) 
							& FileAttributes.System) == FileAttributes.System)//File is system
						{
							// Unsystem the file.
							File.SetAttributes(path, (File.GetAttributes(path) ^ FileAttributes.System));
						}
					}				
					
				}
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
		public FileAttributes GetAttribute( string path )
		{
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !Directory.Exists( path) )
				{
					return FileAttributes.Device;
				}
			}
			else//file name is opposite path
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				return File.GetAttributes( path );
			}
			else
			{
				return FileAttributes.Device;
			}
		}
		public void Run(string path)
		{
			if( path.IndexOf(":") == 1 )//file name is full path
			{
				if( !File.Exists( path) )
				{
					Console.Write( "文件'" + path + "'不存在\n");		
					return;
				}
			}			
			else//file name is opposite path
				
			{
				path = Directory.GetCurrentDirectory() + @"\" + path;
			}
			if( File.Exists( path ) )
			{
				 System.Diagnostics.Process.Start( path );									
			}
			else
			{
				Console.Write( "文件'" + Path.GetFullPath( path) + "'不存在\n");		
				return;
			}
		}
	}
}

⌨️ 快捷键说明

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