错误 1053:服务没有及时响应启动或控制请求
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Ionic.Utils.Zip;
using System.IO;
using System.Threading;
using SendEmail;namespace LBBWindowsService
{
    public partial class Service1 : ServiceBase
    {        private static string path = @"Log\" + DateTime.Now.ToString("yyyy-MM-dd");
        private static string zippath;
        private static string unzippath;
        private static string pathbak;
        private static string unpathbak;//错误的文件路径
        private static string pathMove;
        private static int scodeLength;//如果店铺编号长度改变,修改ScodeLength.ini配置文件中的值
        private static string fromEmail;//发送者邮箱地址
        private static string fromPsaaWord;//发送者邮箱密码
        private static string toEmail;//收件人邮箱地址
        private static string subject;//邮件主题
        private static string body;//邮件主要内容
        private static string[] toEmpEmail = null;
        private static string[] attachmentsPath = null;
                 public Service1()
        {
            InitializeComponent(); 
            bind();
        }
         protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            //创建日志文件夹(没有就创建)
            IOTPA.Create(path);            //程序退出写日志
            IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 解析服务开启;"); 
 
            this.timer1.Enabled = true; 
        }        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 
            //创建日志文件夹(没有就创建)
            IOTPA.Create(path); 
            //程序退出写日志
            IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 解析服务--停止;");             this.timer1.Enabled = false;
        }        //protected override void OnPause()
        //{
        //    //程序退出写日志
        //    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 解析服务--暂停;");        //    this.timer1.Enabled = false;
        //}        //protected override void OnContinue()
        //{
        //    //程序退出写日志
        //    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 解析服务--恢复;");        //    this.timer1.Enabled = true;
        //}
         
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer1.Stop();
            CopyDirectory(pathMove, zippath);
            unzip(zippath, unzippath);
            WriteSql(unzippath);
            this.timer1.Start();
        }        /// <summary>
        /// 程序启动调用
        /// </summary>
        protected void bind()
        {
            if (IOTPA.readTextFile("ServerIP.ini").Trim() != "")
            {
                try
                {
                    //读取解析配置文件    
                    INIClass Sinc = new INIClass(Application.StartupPath + "//ServerIP.ini");                    zippath = Sinc.IniReadValue("server", "zippath");//要解压的 (.ZIP) 文件路径
                    if (zippath == "")
                    {
                        Sinc.IniWriteValue("server", "zippath", "Log\\choicedata");
                    }
                    pathMove = Sinc.IniReadValue("server", "pathMove");//上传的压缩文件路径
                    if (pathMove == "")
                    {
                        Sinc.IniWriteValue("server", "pathMove", "E:\\ceshidate\\data");
                    }
                    unzippath = Sinc.IniReadValue("server", "unzippath");//解压后文件的路径
                    if (unzippath == "")
                    {
                        Sinc.IniWriteValue("server", "unzippath", "Log\\dataunzip\\");
                    }
                    pathbak = Sinc.IniReadValue("server", "pathbak");//压缩文件备份的路径
                    if (pathbak == "")
                    {
                        Sinc.IniWriteValue("server", "pathbak", "Log\\datatbak");
                    }
                    unpathbak = Sinc.IniReadValue("server", "Errorpathbak");//压缩文件备份错误文件的路径
                    if (unpathbak == "")
                    {
                        Sinc.IniWriteValue("server", "Errorpathbak", "Log\\Errorpathbak\\");
                    }                    if (Sinc.IniReadValue("server", "scodelength") == "")
                    {
                        Sinc.IniWriteValue("server", "scodelength", "5");
                    }
                    scodeLength = Convert.ToInt32(Sinc.IniReadValue("server", "scodelength"));//店铺编号的长度                     DbHelperOra.connectionString = Sinc.IniReadValue("server", "connectionString");//获取数据库连接
                    if (DbHelperOra.connectionString == "")
                    {
                        Sinc.IniWriteValue("server", "connectionString", "Data Source=;User ID=POSTELEMGRNEW;Password=POSTELEMGRNEW;Unicode=True;");
                    }
                    //获取邮件信息
                    fromEmail = Sinc.IniReadValue("Email", "fromEmail");//发送者邮箱地址
                    if (fromEmail == "")
                    {
                        Sinc.IniWriteValue("Email", "fromEmail", "[email protected]");
                    }
                    fromPsaaWord = Sinc.IniReadValue("Email", "fromPsaaWord");//发送者邮箱密码
                    if (fromPsaaWord == "")
                    {
                        Sinc.IniWriteValue("Email", "fromPsaaWord", "choiceERROR2011,");
                    }
                    toEmail = Sinc.IniReadValue("Email", "toEmail");//收件人邮箱地址
                    if (toEmail == "")
                    {
                        Sinc.IniWriteValue("Email", "toEmail", "[email protected],[email protected],[email protected]");
                    }
                    subject = Sinc.IniReadValue("Email", "subject");//邮件主题
                    if (subject == "")
                    {
                        Sinc.IniWriteValue("Email", "subject", "上传的数据错误");
                    }
                    body = Sinc.IniReadValue("Email", "body");//邮件内容
                    if (body == "")
                    {
                        Sinc.IniWriteValue("Email", "body", "上传的数据错误");
                    }                    //创建日志文件夹(没有就创建)
                    IOTPA.Create(path);
                    IOTPA.Create(pathbak);
                    IOTPA.Create(unpathbak);
                    //程序启动写日志
                    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 解析服务开启;");                }
                catch
                {
                    MessageBox.Show("请检查ScodeLength.ini文件中scodelength节点保存的是否为整形的数字,该文件保存的是店铺编号SCODE的长度,请准确填写后重启该程序!");
                    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 系统中ScodeLength.ini配置文件的内容有问题,该文件保存的是店铺编号(SCODE)的字符长度,为数字类型;");
                }
                //this.timer1.Start(); 
            }
            else
            {
                MessageBox.Show("请检查ScodeLength.ini文件是否存在,该文件保存的是店铺编号SCODE的长度,请准确填写后重启该程序!");
                IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 系统中缺少ScodeLength.ini配置文件,该文件保存的是店铺编号(SCODE)的字符长度,为数字类型;");
            }
        }

解决方案 »

  1.   

            /// <summary>
            /// 读文件写到数据库中
            /// </summary>
            /// <param name="path"></param>
            protected void WriteSql(string unpath)
            {
                SendEmailTo m_send = new SendEmailTo();
                StringBuilder strSql = new StringBuilder();
                string unbox = unpath;            if (Directory.Exists(unbox))
                {
                    string[] filename = Directory.GetFiles(unpath);
                    if (filename.Length > 0)
                    {
                        int a = 0;
                        int aa;
                        string fname;
                        string scode;
                        string time;
                        string year;
                        string moth;
                        string day;
                        string sql = "";
                        for (int i = 0; i < filename.Length; i++)
                        {
                            if (filename[i].ToUpper().EndsWith(".SQL"))
                            {
                                ArrayList list = IOTPA.Reader(filename[i].ToString());
                                if (list != null)
                                {
                                    if (list.Count > 0)
                                    {
                                        for (int k = 0; k < list.Count; k++)
                                        {
                                            strSql.Append(list[k].ToString());
                                        }
                                        string[] lists = strSql.ToString().Split(';');                                    a = filename[i].LastIndexOf("\\") + 1;
                                        scode = filename[i].Substring(a, scodeLength);//店铺编号
                                        time = filename[i].Substring(a + scodeLength, 8);
                                        year = time.Substring(0, 4);
                                        moth = time.Substring(4, 2);
                                        day = time.Substring(6);
                                        time = year + "-" + moth + "-" + day;
                                        try
                                        {
                                            time = Convert.ToDateTime(time).ToString("yyyy-MM-dd");
                                        }
                                        catch
                                        {
                                            IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMMdd") + "Info.log", DateTime.Now.ToString() + " 上传上来的" + filename[i].ToString() + "文件名称有问题,请检查店铺编号的长度是否与配置文件中的相对应!");
                                        }
                                        try
                                        {
                                            ArrayList array = new ArrayList(lists);
                                            if (DbHelperOra.ExecuteSqlTrans(array))
                                            {
                                                IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 向数据库中插入" + filename[i].ToString() + "文件的内容OK");
                                                File.Delete(filename[i].ToString());                                            sql = "delete from sys_workdate where workdate=to_date('" + time + "','yyyy-MM-dd') and scode='" + scode + "'";
                                                DbHelperOra.ExecuteSql(sql);//删除
                                                sql = "insert into sys_workdate (workdate,scode,isupload1) values(to_date('" + time + "','yyyy-MM-dd'),'" + scode + "',1)";
                                                if (DbHelperOra.ExecuteSql(sql) > 0)
                                                {
                                                    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 向数据库中插入" + filename[i].ToString() + "文件的内容成功,数据上传OK");
                                                }
                                            }
                                            else
                                            {
                                                IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMMdd") + "Info.log", DateTime.Now.ToString() + " 向数据库中插入" + filename[i].ToString() + "文件数据时,没有插入进去");                                            aa = filename[i].LastIndexOf("\\") + 1;
                                                fname = filename[i].Substring(aa);
                                                File.Copy(filename[i].ToString(), unpathbak + fname, true);                                            attachmentsPath = new string[1];//邮件附件(无附件时为null) 
                                                attachmentsPath[0] = filename[i].ToString();                                            toEmpEmail = toEmail.Split(',');
                                                subject = subject + "文件名称为:" + fname;
                                                body = body + "文件名称为:" + fname;
                                                for (int j = 0; j < toEmpEmail.Length; j++)
                                                {
                                                    try
                                                    {
                                                        m_send.SendEmail(fromEmail, fromPsaaWord, toEmpEmail[j].ToString(), subject, body, attachmentsPath);
                                                    }
                                                    catch
                                                    {
                                                        IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 发送邮件时出现异常,请检查邮箱是否正确!");
                                                    }
                                                }                                            File.Delete(filename[i].ToString());
                                                sql = "delete from sys_workdate where workdate=to_date('" + time + "','yyyy-MM-dd') and scode='" + scode + "'";
                                                DbHelperOra.ExecuteSql(sql);//删除
                                                sql = "insert into sys_workdate (workdate,scode,isupload1) values(to_date('" + time + "','yyyy-MM-dd'),'" + scode + "',0)";
                                                if (DbHelperOra.ExecuteSql(sql) > 0)
                                                {
                                                    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMMdd") + "Info.log", DateTime.Now.ToString() + " 向数据库中插入" + filename[i].ToString() + "文件数据时,没有插入进去,数据上传失败!");
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMMdd") + "Info.log", DateTime.Now.ToString() + " 向数据库中插入" + filename[i].ToString() + "文件数据时出现异常,请检查该文件");                                        aa = filename[i].LastIndexOf("\\") + 1;
                                            fname = filename[i].Substring(aa);
                                            File.Copy(filename[i].ToString(), unpathbak + fname, true);                                        attachmentsPath = new string[1];//邮件附件(无附件时为null) 
                                            attachmentsPath[0] = filename[i].ToString();                                        toEmpEmail = toEmail.Split(',');
                                            subject = subject + "文件名称为:" + fname;
                                            body = body + "文件名称为:" + fname;
                                            for (int j = 0; j < toEmpEmail.Length; j++)
                                            {
                                                try
                                                {
                                                    m_send.SendEmail(fromEmail, fromPsaaWord, toEmpEmail[j].ToString(), subject, body, attachmentsPath);
                                                }
                                                catch
                                                {
                                                    IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 发送邮件时出现异常,请检查邮箱是否正确!");
                                                }
                                            }                                        File.Delete(filename[i].ToString());                                        sql = "delete from sys_workdate where workdate=to_date('" + time + "','yyyy-MM-dd') and scode='" + scode + "'";
                                            DbHelperOra.ExecuteSql(sql);//删除
                                            sql = "insert into sys_workdate (workdate,scode,isupload1) values(to_date('" + time + "','yyyy-MM-dd'),'" + scode + "',0)";
                                            if (DbHelperOra.ExecuteSql(sql) > 0)
                                            {
                                                IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMMdd") + "Info.log", DateTime.Now.ToString() + " 向数据库中插入" + filename[i].ToString() + "文件数据时出现异常,数据上传失败!");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
      

  2.   


            public static void zip()
            {
                //压缩
                using (ZipFile zip = new ZipFile("posdata/img.zip"))
                {
                    zip.AddDirectory("posdata/img");
                    zip.Save();
                }        }        /// <summary>
            /// 解压
            /// </summary>
            /// <param name="path"></param>
            /// <param name="unpath"></param>
            public static void unzip(string paths, string unpath)
            {
                SendEmailTo m_send = new SendEmailTo();
                string fname = "";
                //unpath = unpath + @"\";
                if (Directory.Exists(paths))
                {
                    string[] zips = Directory.GetFiles(paths);
                    if (zips.Length > 0)
                    {
                        for (int i = 0; i < zips.Length; i++)
                        {
                            if (zips[i].ToUpper().EndsWith(".ZIP"))
                            {
                                try
                                {
                                    //解压
                                    ZipFile zip = ZipFile.Read(zips[i].ToString());                                foreach (ZipEntry e in zip)
                                    {
                                        try
                                        {
                                            e.Extract(unpath, true); // overwrite == true 
                                        }
                                        catch
                                        {
                                            IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 解压" + zips[i].ToString() + "文件时出现异常");
                                        }
                                    }
                                    try
                                    {
                                        fname = zip.Name.Substring(zip.Name.LastIndexOf("\\"));//得到文件名称
                                        File.Copy(zips[i].ToString(), pathbak + fname, true);
                                        File.Delete(zips[i].ToString());
                                    }
                                    catch
                                    {
                                        IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 移动" + zips[i].ToString() + "文件时出现异常");
                                    }                            }
                                catch
                                {
                                    int aa = zips[i].LastIndexOf("\\") + 1;
                                    fname = zips[i].Substring(aa);
                                    File.Copy(zips[i].ToString(), unpathbak + fname, true);                                attachmentsPath = new string[1];//邮件附件(无附件时为null) 
                                    attachmentsPath[0] = zips[i].ToString();                                toEmpEmail = toEmail.Split(',');                                subject = subject + "文件名称为:" + fname;
                                    body = body + "文件名称为:" + fname;
                                    for (int j = 0; j < toEmpEmail.Length; j++)
                                    {
                                        try
                                        {
                                            m_send.SendEmail(fromEmail, fromPsaaWord, toEmpEmail[j].ToString(), subject, body, attachmentsPath);
                                        }
                                        catch
                                        {
                                            IOTPA.writeText(path + @"\" + DateTime.Now.ToString("yyyyMM") + "RUN.log", DateTime.Now.ToString() + " 发送邮件时出现异常,请检查邮箱是否正确!");
                                        }                                }                                GC.Collect();
                                    try
                                    {
                                        File.Delete(zips[i].ToString());
                                    }
                                    catch
                                    { }
                                }
                            }
                        }
                    }
                } 
            }
            /// <summary>
            /// 文件夹复制函数编写
            /// </summary>
            /// <param name="source">被复制的文件路径</param>
            /// <param name="destination">新的文件路径</param>
            public static void CopyDirectory(String source, String destination)
            {
                DirectoryInfo info = new DirectoryInfo(source);
                foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
                {
                    //目标路径destName = 目标文件夹路径 + 原文件夹下的子文件(或文件夹)名字
                    //Path.Combine(string a ,string b) 为合并两个字符串
                    String destName = Path.Combine(destination, fsi.Name);
                    //如果是文件类,就复制文件
                    if (fsi is System.IO.FileInfo)
                    {
                        try
                        {
                            if (!Directory.Exists(destination))//如果文件夹不存在就创建
                            {
                                Directory.CreateDirectory(destination);
                            }                        File.Copy(fsi.FullName, destName, true);
                            //如果不是 则为文件夹,继续调用文件夹复制函数,递归
                            File.Delete(Path.Combine(source, fsi.Name));
                        }
                        catch
                        {                    }
                    }
                    else
                    {
                        Directory.CreateDirectory(destName);
                        CopyDirectory(fsi.FullName, destName);
                    }
                }
            }
        }
    }
      

  3.   

    找到问题了,是读写文件路径的问题 用 Application.StartupPath 就可以了