fileoperate.cs

来自「中国海洋大学211工程网站的全部源码」· CS 代码 · 共 99 行

CS
99
字号
using System;
using System.Web;
using System.IO;
using System.Text;

namespace _211
{
	/// <summary>
	/// FileOperate 的摘要说明。
	/// </summary>
	public sealed class FileOperate
	{
		public static string UpLoadFile(System.Web.UI.HtmlControls.HtmlInputFile upfile)
		{
			string f_type=upfile.PostedFile.FileName.Substring(upfile.PostedFile.FileName.LastIndexOf(".")+1).ToLower();
			
			bool chk=true;
			using(DownType dt=new DownType(f_type,true))
			{
				if(dt.HasRecord)chk=false;
			}

			if(chk)
			{
				throw new Exception("上传文件类型受限制");
			}

			string savename=HttpContext.Current.Server.MapPath("upload/");

			string path=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+"."+f_type;
			savename+=path;
			try
			{
				upfile.PostedFile.SaveAs(savename);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			return "upload/"+path;
		}

		public static void DelFile(string key)
		{
			string fn=HttpContext.Current.Server.MapPath(key);
			FileInfo fi=new FileInfo(fn);
			if(fi.Exists)
			{
				try
				{
					fi.Delete();
				}
				catch(Exception ex)
				{
					throw ex;
				}
			}
		}

		public static string ReadModel(string FileName)
		{
			string re="";
			string path=HttpContext.Current.Server.MapPath("model/");
			StreamReader sr=new StreamReader(path+FileName+".model",System.Text.Encoding.GetEncoding("gb2312"));
			try
			{
				re=sr.ReadToEnd();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally
			{
				sr.Close();
			}
			return re;
		}

		public static void AddStaticWeb(StringBuilder sb,string FileName)
		{
			string path=HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString();
			StreamWriter sw=new StreamWriter(path+FileName+".htm",false,System.Text.Encoding.GetEncoding("gb2312"));
			try
			{
				sw.Write(sb.ToString());
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally
			{
				sw.Close();
			}
		}
	}
}

⌨️ 快捷键说明

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