?? 代碼.txt
字號(hào):
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Configuration;
using System.Drawing;
namespace zhuanti
{
/// <summary>
/// 這是一個(gè)用于玩家投稿中實(shí)現(xiàn)玩家上傳文件功能中用到的相應(yīng)的函數(shù)的功能模塊
/// </summary>
public class FileUpload
{
public enum File //定義一個(gè)人用于存放玩家上傳文件信息的一個(gè)數(shù)組
{
FILE_SIZE , //大小
FILE_POSTNAME, //類(lèi)型(文件后綴名)
FILE_SYSNAME , //系統(tǒng)名
FILE_ORGINNAME, //原來(lái)的名字
FILE_PATH //文件路徑
}
private static Random rnd = new Random(); //獲取一個(gè)隨機(jī)數(shù)
public static string[] UploadFile(HtmlInputFile file,string Upload_Dir) //實(shí)現(xiàn)玩家文件上傳功能的主函數(shù)
{
string[] arr = new String[5];
string FileName = GetUniquelyString(); //獲取一個(gè)不重復(fù)的文件名
string FileOrginName = file.PostedFile.FileName.Substring
(file.PostedFile.FileName.LastIndexOf("\\")+1);//獲取文件的原始名
if(file.PostedFile.ContentLength<=0)
{ return null; }
string postFileName;
string FilePath = Upload_Dir.ToString();
string path = FilePath + "\\";
try
{
int pos = file.PostedFile.FileName.LastIndexOf(".")+1;
postFileName = file.PostedFile.FileName.Substring(pos,file.PostedFile.FileName.Length-pos);
file.PostedFile.SaveAs(path+FileName+"."+postFileName); //存儲(chǔ)指定的文件到指定的目錄
}
catch(Exception exec)
{
throw(exec);
}
double unit = 1024;
double size = Math.Round(file.PostedFile.ContentLength/unit,2);
arr[(int)File.FILE_SIZE] = size.ToString(); //文件大小
arr[(int)File.FILE_POSTNAME] = postFileName; //文件類(lèi)型(文件后綴名)
arr[(int)File.FILE_SYSNAME] = FileName; //文件系統(tǒng)名
arr[(int)File.FILE_ORGINNAME] = FileOrginName; //文件原來(lái)的名字
arr[(int)File.FILE_PATH]=path+FileName+"."+postFileName; //文件路徑
return arr;
}
public static bool OperateDB(string sqlstr) //建立一個(gè)和數(shù)據(jù)庫(kù)的關(guān)聯(lián)
{
if (sqlstr==String.Empty)
return false;
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings
["connstring"]);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
return true;
}
public static string GetUniquelyString() //獲取一個(gè)不重復(fù)的文件名
{
const int RANDOM_MAX_VALUE = 1000;
string strTemp,strYear,strMonth,strDay,strHour,strMinute,strSecond,strMillisecond;
DateTime dt =DateTime.Now;
int rndNumber = rnd.Next(RANDOM_MAX_VALUE);
strYear = dt.Year.ToString ();
strMonth = (dt.Month > 9)? dt.Month.ToString() : "0" + dt.Month.ToString();
strDay = (dt.Day > 9)? dt.Day.ToString() : "0" + dt.Day.ToString();
strHour = (dt.Hour > 9)? dt.Hour.ToString() : "0" + dt.Hour.ToString();
strMinute = (dt.Minute > 9)? dt.Minute.ToString() : "0" + dt.Minute.ToString();
strSecond = (dt.Second > 9)? dt.Second.ToString() : "0" + dt.Second.ToString();
strMillisecond = dt.Millisecond.ToString();
strTemp = strYear + strMonth + strDay +"_"+ strHour + strMinute + strSecond +"_"+
strMillisecond +"_"+ rndNumber.ToString () ;
return strTemp;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -