安装Windows服务时不显示NotifyIcon

时间:2014-12-12 06:24:56

标签: c# windows-services notifyicon

我创建了一个与以下编码相同的Windows服务。该服务工作得很好,但问题仅在于WindowsTray上没有显示NotifyIcon。 OnStart服务我用INBOX调用了Timer和timer_Tick事件,并调用了Sent。当我使用IF DEBUG ELSE调试代码时...... NotifyIcon显示在托盘上,但是当我安装服务时,它没有显示。 不知道问题是什么?

public partial class Service1 : ServiceBase
    {
        private Timer timer1 = null;

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer1 = new Timer();
        this.timer1.Interval = 15000;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, ElapsedEventArgs e)
    {
        GetSentItems();
        GetInbox();
    }

    protected override void OnStop()
    {
        timer1.Enabled = false;
    }

    #region GET SENT ITEMS
    public void GetSentItems()
    {
        var client = new ImapClient("imap.xyz.com", false);
        var folderPath = "";
        if (client.Connect())
        {
            if (client.Login("abc@xyz.com", "qwerty@123"))
            {
                var folder = client.Folders.Sent;
                var messages = folder.Search("ALL");
                var fileName = "";
                var count = messages.Length;
                foreach (var x in messages)
                {
                    //if (x.Subject.Contains("Request"))
                    //{
                    folderPath = @"C:\Attachment\SentItems";
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    foreach (var file in x.Attachments)
                    {
                        //file.Download(); // Downloading the attachment                    
                        fileName = file.FileName;
                        //file.Save(folderPath);
                    }
                    var Date = x.Date.Value;
                    var From = x.From.Address;
                    var to = x.To[0].Address;
                    var subject = x.Subject;
                    // var attchment = x.Attachments[0].FileData;
                    var Body = x.BodyParts[0].ContentStream;
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = "#constring";
                    string textbody = Body;
                    textbody = "'" + textbody.Replace("'", "") + "'";
                    string query1 = "SP_SentEmail";
                    SqlCommand cmd1 = new SqlCommand(query1, con);
                    cmd1.CommandType = CommandType.StoredProcedure;
                    cmd1.Parameters.AddWithValue("@E_Date", Date);
                    cmd1.Parameters.AddWithValue("@E_From", From);
                    cmd1.Parameters.AddWithValue("@E_TextBody", textbody);
                    cmd1.Parameters.AddWithValue("@Subject", subject);
                    cmd1.Parameters.AddWithValue("@E_attachments", fileName);
                    con.Open();
                    cmd1.ExecuteNonQuery();
                    con.Close();


                }
            }
        }
    }
    #endregion

    #region GET INBOX
    public void GetInbox()
    {
        var client = new ImapClient("imap.xyz.com", false);
        var folderPath = "";
        if (client.Connect())
        {
            if (client.Login("abc@xyz.com", "qwerty@123"))
            {
                var folderInbox = client.Folders.Inbox;
                var messagesInbox = folderInbox.Search("UNSEEN");
                var fileNameInbox = "";
                var countInbox = messagesInbox.Length;
                foreach (var x in messagesInbox)
                {
                    //if (x.Subject.Contains("Request"))
                    //{
                    folderPath = @"C:\Attachment\Inbox";
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    foreach (var file in x.Attachments)
                    {
                        //file.Download(); // Downloading the attachment                    
                        fileNameInbox = file.FileName;
                        //file.Save(folderPath);
                    }
                    var Date = x.Date.Value;
                    var From = x.From.Address;
                    var to = x.To[0].Address;
                    var subject = x.Subject;
                    var Body = x.BodyParts[0].ContentStream;
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = "Data Source....";
                    string textbody = Body;
                    textbody = "'" + textbody.Replace("'", "") + "'";
                    string query2 = "SP_EmailInbox";
                    SqlCommand cmd2 = new SqlCommand(query2, con);
                    cmd2.CommandType = CommandType.StoredProcedure;
                    cmd2.Parameters.AddWithValue("@E_Date", Date);
                    cmd2.Parameters.AddWithValue("@E_From", From);
                    cmd2.Parameters.AddWithValue("@E_TextBody", textbody);
                    cmd2.Parameters.AddWithValue("@Subject", subject);
                    cmd2.Parameters.AddWithValue("@E_attachments", fileNameInbox);
                    cmd2.Parameters.Add("@Inserted", SqlDbType.Bit).Direction = ParameterDirection.Output;
                    con.Open();
                    cmd2.ExecuteNonQuery();
                    con.Close();
                    bool IsInserted = Convert.ToBoolean(cmd2.Parameters["@Inserted"].Value);
                    if (IsInserted == true)
                    {
                        notifyIcon1.Icon = SystemIcons.Application;
                        notifyIcon1.BalloonTipTitle = "Mail Application";
                        notifyIcon1.BalloonTipText = "New Mail: " + subject.Substring(0, subject.Length);
                        notifyIcon1.ShowBalloonTip(1000);
                    }
                }
            }
        }
    #endregion


    }
}

0 个答案:

没有答案