asp.ent c#开发有用的方法

一、         上传文件处理... 1

二、         上传文件操作... 3

三、         待研究的删除方法... 4

四、         C#弹出javasript窗口的方法... 6

五、         处理String的方法... 6

六、         asp.net调用javasript显示提示框... 7

                                                                                                  

 

 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EMP.MySQLUtility;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;

namespace File
{

一、  上传文件处理

    /// <summary>
    /// General
的摘要说明。

    ///
发布日期:2002-8-8 原作者:雷神
    ///
此程序属模式小组 HTTP://WWW.AI361.COM/PROJECT/
    ///
sql2000WIN2000s+.Net+iis5中测试通过
    /// </summary>
    public class FileManage
    {
        private string FilePath; //
文件路径

        //定义一个枚举用来存放文件的信息       
        public enum File
        {
            FILE_SIZE,        //
大小

            FILE_POSTNAME,    // 
文件类型(文件后缀名)
            FILE_SYSNAME,      //
文件系统名
            FILE_ORGINNAME,//
文件原来的名字
            FILE_PATH                //
文件路径
        };
        //
构造函数
        public FileManage()
        {
            //
WEB.CONFIG中设定AppSettings["filepath"],用于存放文件的路径。
            //FilePath = System.Configuration.ConfigurationSettings.AppSettings["filepath"];
            //  string filepath = SyMapPath(path);//
得到web服务器上的物理路径
            FilePath = System.Configuration.ConfigurationSettings.AppSettings["filepath"];
            // System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
        }

        /// <summary>
        ///
预期上传文件通用函数,此方法为静态,系统任何时候均可调用。

        /// </summary>
        /// <param name="file">
参数为页面的FILE控件对象</param>
        /// <returns>
文件属性
</returns>
        public static EMP.Model.FileInfo UploadFile(FileUpload pfile)
        {
            // string[] arr = new String[5];
            //
通过系统时间加两位随即数生成文件名,。

            //string FileName = DateTime.Now.ToString().Replace(" ","").Replace(":","").Replace("-","");
            //string FileName = DateTime.Now.ToString().Replace(" ", "").Replace(":", "-");
            string dateTime = DateTime.Now.ToString().Replace(" ", "").Replace(":", ".");
            Random rnd = new Random();
            int iRandNum = rnd.Next(10);
            int iRandNum2 = rnd.Next(10);
            string FileName = dateTime + iRandNum + iRandNum2;
            string FileOrginName = pfile.PostedFile.FileName.Substring(pfile.PostedFile.FileName.LastIndexOf("//") + 1);
            if (pfile.PostedFile.ContentLength <= 0)
                return null;
            string postFileName;
            string path = new FileManage().FilePath + "//";
            try
            {

                int pos = pfile.PostedFile.FileName.LastIndexOf(".") + 1;
                postFileName = pfile.PostedFile.FileName.Substring(pos, pfile.PostedFile.FileName.Length - pos);
                //file.PostedFile.SaveAs(path + FileName + "." + postFileName);
            }
            catch (Exception exec)
            {
                throw (exec);
            }
            #region
            //  double unit = 1024;
            //  double size = Math.Round(pfile.PostedFile.ContentLength / unit, 2);
            // arr[(int)File.FILE_SIZE] = size.ToString();//
文件大小

            //   arr[(int)File.FILE_POSTNAME] = postFileName;//
文件类型(文件后缀名)
            // arr[(int)File.FILE_SYSNAME] = FileName;//
文件系统名
            //arr[(int)File.FILE_ORGINNAME] = FileOrginName;//
文件原来的名字
            //  arr[(int)File.FILE_PATH] = path + FileName + "." + postFileName;//
文件路径
            #endregion
            string FileOrginPath = pfile.PostedFile.FileName;
            EMP.Model.FileInfo fileInfo = new EMP.Model.FileInfo(path + FileName + "." + postFileName, FileOrginName, FileOrginPath);
            return fileInfo;
                   }

 

二、  上传文件操作

        /// <summary>
        /// 
上传文件

        /// </summary>
        /// <param name="pFilePath"></param>
        /// <returns></returns>
        public static bool UploadFile(string pOrgFilePath, string pFilePath)
        {
            bool upStaut = false;
            try
            {
                if (System.IO.File.Exists(pOrgFilePath))
                {
                    System.IO.File.Copy(pOrgFilePath, pFilePath, true);
                    upStaut = true;
                }

            }
            catch (Exception ex)
            {
                upStaut = false;
            }
            return upStaut;
            //throw(new Exception(HtmlUtility.HtmlEncode(IDNO.PostedFile.FileName)));
        }

三、  待研究的删除方法

        /*  事实上,调用File.Delete并没有删除文件,它只是让操作系统认为文件不存在,文件在磁盘上的空间被标记成空的,以便用于再次使用。但是文件的数据没有被移除,您可以非常容易恢复。被删除的文件直到相应的空间被重写才会真消失,这也许要很长时间。

         作者提供了一个简单的类,它被用来在删除文件之前向文件写入垃圾数据,这样就令文件的数据不再存在。
        
为了实现这一步,需将文件大小置为0字节,并且修改了文件日期。不过,修改文件日期只能在FAT16/32上有效,在NTFS上就没效果。

         在使用Ontrack EasyRecoveryPC Inspector File RecoveryGetDataBack测试后,已经做到不能恢复任何文件了。在删除一个文本文件后,我即使运行Investigator,确认独立的簇,还是无法从这文件得到一个文字。

          代码
         
你只需要调用一个方法:WipeFile,下面是这个方法的代码:
         
如果你调用了WipeFile,你需要提供要删除文件的绝对路径和你想重写数据的次数。 */
        public static void WipeFile(string filename, int timesToWrite)
        {
            try
            {
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.SetAttributes(filename, System.IO.FileAttributes.Normal); //
设置文件的属性为正常,这是为了防止文件是只读

                    double sectors = Math.Ceiling(new System.IO.FileInfo(filename).Length / 512.0); //
计算文件中的扇区数目
                    byte[] dummyBuffer = new byte[512]; //
创建一个扇区大小的虚拟缓存
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); //
创建一个加密随机数目生成器这是我用来生成垃圾数据的。
                    System.IO.FileStream inputStream = new System.IO.FileStream(filename, FileMode.Open);  //
打开这个文件的FileStream
                    for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
                    {
                        //UpdatePassInfo(currentPass + 1, timesToWrite);
                        inputStream.Position = 0;
                        //
循环所有的扇区

                        for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
                        {
                            //   UpdateSectorInfo(sectorsWritten + 1, (int)sectors);
                            //
把垃圾数据填充到流中
                            rng.GetBytes(dummyBuffer);
                            inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                            sectorsWritten++;
                        }
                    }
                    inputStream.SetLength(0);
                    inputStream.Close();
                    //
原始日期需要清空
                    DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
                    System.IO.File.SetCreationTime(filename, dt);
                    System.IO.File.SetLastAccessTime(filename, dt);
                    System.IO.File.SetLastWriteTime(filename, dt);
                    System.IO.File.Delete(filename); //
最后删除文件

                    //WipeDone();
                }
            }
            catch (Exception e)
            {
                //   WipeError(e);
            }
        }

        public static void delFile(string filepath)
        {
            if (System.IO.File.Exists(filepath))
                System.IO.File.Delete(filepath);
        }

 

四、  C#弹出javasript窗口的方法

        /// <summary>
        ///
弹出一个新的窗口

        /// </summary>
        /// <param name="opener"></param>
        /// <param name="PagePath"></param>
        /// <param name="windowName"></param>
        /// <param name="Top"></param>
        /// <param name="Left"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void OpenPopUp(System.Web.UI.WebControls.WebControl opener, string PagePath, string windowName, int Top, int Left, int width, int height)
        {
            string clientScript;
            string windowAttribs;
            windowAttribs = "left=" + Left.ToString() + "px," +
            "top=" + Top.ToString() + "px," +
            "width=" + width.ToString() + "px," +
            "height=" + height.ToString() + "px," +
            "left='+((screen.width -" + width.ToString() + ") / 2)+',";
            clientScript = "window.open('" + PagePath + "','" +
            windowName + "','" + windowAttribs + "');return false;";
            opener.Attributes.Add("onClick", clientScript);
        }

五、  处理String的方法

        /// <summary>
        ///
textbox取出收信人

        /// </summary>
        /// <returns>
收信人字符数组</returns>
        public static string[] GetReceiverInTextbox(string pText)
        {
            string[] Reces = null;
            pText = pText.Trim();     //
除去空格

            pText = pText.Replace("", ",");//替换,符合
            //if (str2.Contains(",")) 
            if (!string.IsNullOrEmpty(pText))
                if (pText[pText.Length - 1].Equals(','))
                {
                    Reces = pText.Split(','); //
取出收信人
                }
            return Reces;
        }

 

 

六、  asp.net调用javasript显示提示框

        /// <summary>
        ///
显示提示框

        /// </summary>
        /// <param name="pClientScript"></param>
        /// <param name="pMessage"></param>

        public static void showScript(System.Web.UI.Page pPage, string pMessage)
        {
            pPage.ClientScript.RegisterStartupScript(pPage.GetType(), "", "<script language = javascript>alert('" + pMessage + "')</script>");
        }
    }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值