功能是每隔一段时间(通过任务计划实现)服务器自动打开这个页面,然后自动备份数据并打压缩包,再自动传送到ftp服务器上去。
目前我不能执行这个页面,但是他可以完成后自动关闭,我看了代码,不知道为什么会自动把IE关闭掉。
后台代码:(前台没任何特殊性)
using System;
using System.IO;
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 System.Diagnostics;
using System.Net;public partial class BackupDB_Default : System.Web.UI.Page
{
    #region Define Variables
    bool isSuccess = false;    string ftpServerIP = "101.101.10.104";//Remote Server
    int ftpPort = 21;
    string ftpUserID = "T";
    string ftpPassword = "abcdef11";    string pBackupDBAndCompressFilesPath = @"D:\DataCenter";
    string pBackupDBFileName = "Iridian2#168EW@370$.bak";
    string pCompressFileName = "Iridian2#168EW@370$.rar";
    #endregion    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Directory.Exists(pBackupDBAndCompressFilesPath))
                BackupDatabase();
        }
    }
    #endregion    #region BackupDatabase
    private void BackupDatabase()
    {
        //SQLDMO.dll is a COM Object ,Microsoft SQl Server provider
        SQLDMO.Backup pBackup = new SQLDMO.BackupClass();
        SQLDMO.SQLServer pSqlServer = new SQLDMO.SQLServerClass();        try
        {
            pSqlServer.LoginSecure = false;
            pSqlServer.Connect("110.110.10.5", "sa", "1111");//Server IP,Database Login UserName and Password
            pBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
            pBackup.Database = "Iridian2";
            pBackup.Files = pBackupDBAndCompressFilesPath + "\\" + pBackupDBFileName;
            pBackup.BackupSetName = "Iridian2";
            pBackup.BackupSetDescription = "Database Backup";
            pBackup.Initialize = true;
            pBackup.SQLBackup(pSqlServer);
            isSuccess = true;
        }
        catch (Exception ex)
        {
            isSuccess = false;
            Response.Write(ex.Message);
        }
        finally
        {
            pSqlServer.DisConnect();            if (isSuccess)
            {
                bool isCompressSuccess = false;                if (File.Exists(pBackupDBAndCompressFilesPath + "\\" + pBackupDBFileName))
                    isCompressSuccess = CompressDBBackupFile(pBackupDBAndCompressFilesPath + "\\" + pBackupDBFileName, pBackupDBAndCompressFilesPath, pCompressFileName);                if (isCompressSuccess)
                SendZipFileToAnotherComputer(pCompressFileName);
            }
        }
    }
    #endregion    #region CompressDBBackupFile
    private bool CompressDBBackupFile(string patch, string rarPatch, string rarName)
    {
        bool excuteSuccess = false;
        String the_Info;
        ProcessStartInfo the_StartInfo;
        Process the_Process = new Process();
        try
        {
            the_Info = " a    " + rarName + "  " + patch + "  -r  -o+  -m5  -pEW#123@";//Set Password:EW#123@
            the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";//Winrar install path
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_StartInfo.WorkingDirectory = rarPatch;
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            excuteSuccess = true;
        }
        catch (Exception ex)
        {
            excuteSuccess = false;
            Response.Write(ex.Message);
        }
        finally
        {
            the_Process.WaitForExit();
            if (the_Process != null)
            {
                the_Process.Close();
                the_Process = null;
            }
        }        return excuteSuccess;
    }
    #endregion    #region SendZipFileToAnotherComputer
    private void SendZipFileToAnotherComputer(string filename)
    {
        FileInfo fileInf = new FileInfo(pBackupDBAndCompressFilesPath + "\\" + filename);
        string uri = "ftp://" + ftpServerIP + ":" + ftpPort.ToString() + "/" + fileInf.Name;//FTP Format:ftp://user:password@ftpserver:port/url-path
        FtpWebRequest reqFTP;        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        reqFTP.UseBinary = true;
        reqFTP.ContentLength = fileInf.Length;        int contentLen;
        int buffLength = 2048;//The buffer size is set to 2kb
        byte[] buff = new byte[buffLength];
        FileStream fs = null;
        Stream strm = null;        try
        {
            fs = fileInf.OpenRead();
            strm = reqFTP.GetRequestStream();
            contentLen = fs.Read(buff, 0, buffLength);
            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }
        }
        catch (Exception ex)
        {
            Response.Write("Upload Error:" + ex.Message);
        }
        finally
        {
            if (strm != null)
            {
                strm.Close();
                strm = null;
            }            if (fs != null)
            {
                fs.Close();
                fs = null;
            }            reqFTP.Abort();
        }
    }
    #endregion
}前台<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="BackupDB_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Backup Database</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    其实完全可以用windows service实现, 不需要页面的
      

  2.   

    现在关键不是如何实现,这个功能已经实现了,就是用web。因为我有另外一个页面,执行完毕也要自动关闭。我自己用的是js方法,但老板希望不要在系统中用不同的方法来处理同样的问题。
    尽管我认为js方法更方便。
    但是我实在不知道这个页面为什么会自己关掉IE。
      

  3.   

    为何不用WinForm做呢,反正都是用任务计划调用
    WinForm多简单,自动关闭直接在Form_Load事件里调用Close()方法就OK了
      

  4.   

    或者是在FtpWebRequest这个类里面有的方法也说不一定,现在公布的代码里面没有哪句是关闭IE的