简体   繁体   English

如何使用Windows Task Scheduler隐藏控制台应用程序窗口

[英]How to hide console application window with windows task scheduler

I've written a small console app that moves backed up databases from one machine to another location via email. 我编写了一个小型控制台应用程序,该应用程序通过电子邮件将备份数据库从一台计算机移至另一位置。

How this works, the machine that the SQL server is on I've got a job created with-in SQL that backs up specific databases to a volume storage that is with-in the same machine but not on the same volume as the Server 2008rC operating system is on. 这是如何工作的,SQL Server所在的计算机我创建了一个包含SQL的作业,该作业将特定数据库备份到与Server 2008rC位于同一计算机但不在同一卷上的卷存储中操作系统已开启。

So, every night about 1am the job runs and backs up specific databases to a volume that I'm only using to back up database specific items. 因此,每天晚上1点左右,作业都会运行并将特定的数据库备份到一个仅用于备份数据库特定项目的卷。 Now, every night about 2am I'm running a windows task that invokes my console application, this console application is the code below an is very simple. 现在,每天晚上大约凌晨2点,我正在运行一个Windows任务,该任务会调用我的控制台应用程序,该控制台应用程序下面的代码非常简单。 All it does is retrieves the databases, zips them all up into one zip file and then emails the backed up databases to a specific email, and from that point the databases get stored at various locations so that I've got a little redundancy. 它所做的就是检索数据库,将它们全部压缩到一个zip文件中,然后将备份的数据库通过电子邮件发送到特定的电子邮件中,从那时起,数据库被存储在不同的位置,因此我有了一点冗余。

Here is my question(s). 这是我的问题。

1.) What is the minimum amount of permissions that I should set on the Root folder that the backed up databases are being stored in so that the windows task scheduler will be able to invoke my console app, and in turn my console app does read, write, and deletes. 1.)我应该在存储备份数据库的Root文件夹上设置的最小权限是多少,以便Windows Task Scheduler能够调用我的控制台应用程序,而我的控制台应用程序确实可以读取,写入和删除。 I would like the minimum amount of permissions set. 我想要最小权限集。

2.) How to I run the console app by hiding the console window so that if an admin is logged on they do not see that window when the console app is invoked via that windows task scheduler. 2.)我如何通过隐藏控制台窗口来运行控制台应用程序,以便在通过该Windows任务计划程序调用控制台应用程序时,如果管理员已登录,则他们不会看到该窗口。 Now, I've been told that I can set my application to a windows forms from a console app and this will fix my window issues, but my question to that is does this mean that every day at 2am this will create a new window and the windows stay active? 现在,有人告诉我可以从控制台应用程序将应用程序设置为Windows窗体,这将解决我的窗口问题,但是我的问题是,这是否意味着每天凌晨2点都会创建一个新窗口,窗户保持活动状态? In other words I need to run this console app, the console app needs to do what it's task is set to do, and I need the console app to complete die out and not be running any longer until 24 hours later at 2am. 换句话说,我需要运行此控制台应用程序,该控制台应用程序需要执行其任务设置的操作,并且我需要该控制台应用程序完成后才能消失,并且要等到凌晨2点24小时后才能运行。

Here is all the code that creates the zip file, emails the zip file, and the deletes the zip file. 这是创建zip文件,通过电子邮件发送zip文件以及删除zip文件的所有代码。 What direction should I take here? 我应该在这里走什么方向?

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

    namespace SendDatabaseMail
    {
        class Program
        {
            static void Main(string[] args)
            {

                string supportEmail = System.Configuration.ConfigurationManager.AppSettings["supportemail"],
                       dataFolder = System.Configuration.ConfigurationManager.AppSettings["datafolder"],
                       zippedFolder = System.Configuration.ConfigurationManager.AppSettings["zippedfolder"],
                       zippedfilename = System.Configuration.ConfigurationManager.AppSettings["zippedfilename"],
                       smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpserver"],
                       smtpPort = System.Configuration.ConfigurationManager.AppSettings["smtpport"],
                       mailSubject = System.Configuration.ConfigurationManager.AppSettings["mailsubject"],
                       mailBody = System.Configuration.ConfigurationManager.AppSettings["mailbody"],
                       mailPassword = System.Configuration.ConfigurationManager.AppSettings["mailpassword"],
                       databasesRepository = System.Configuration.ConfigurationManager.AppSettings["databasesrepository"];


                string repositoryPath = databasesRepository + "/" + dataFolder,
                       zipPath = databasesRepository + "/" + zippedFolder;

                if (Directory.GetFiles(repositoryPath).Length > 0)
                {

                    try
                    {
                        using (System.Net.Mail.MailMessage mm = new MailMessage(new MailAddress(supportEmail), new MailAddress(supportEmail)))
                        {
                            mm.Subject = mailSubject;
                            mm.Body = mailBody;

                            ZipFile.CreateFromDirectory(repositoryPath, zipPath + "/" + zippedfilename);

                            mm.Attachments.Add(new Attachment(zipPath + "/" + zippedfilename));

                            using (SmtpClient smtp = new SmtpClient(smtpServer, short.Parse(smtpPort)))
                            {
                                smtp.Credentials = new System.Net.NetworkCredential(supportEmail, mailPassword);
                                smtp.EnableSsl = true;
                                smtp.Send(mm);
                            }
                        }
                    }
                    catch (Exception ex)
                    { }

                    if (File.Exists(zipPath + "/" + zippedfilename))
                    {
                        File.Delete(zipPath + "/" + zippedfilename);
                    }
                }
            }
        }
    }

I know it is after 3 years, 我知道是三年后

In the Visual Studio, in the project Properties, 在Visual Studio的项目“属性”中,

Select the output type to Windows Application 选择Windows应用程序的输出类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从任务计划程序调试Windows控制台应用程序 - Debugging a Windows Console Application from Task Scheduler 任务计划程序启动时隐藏C#控制台应用程序窗口 - Hide C# Console App Window When Started by Task Scheduler 如何在 Gtk# Windows 应用程序中隐藏控制台窗口 - How to hide console window in Gtk# Windows Application 如何从服务器上的Windows Task Scheduler运行ac#控制台应用程序 - How to run a c# console application from windows task scheduler on server 如何在控制台应用程序中隐藏输出(而不是窗口)? - How to hide output (not the window) in a console application? 任务计划程序失败.Net 4.5控制台应用程序 - Task Scheduler Fails .Net 4.5 Console Application 动态编译Windows应用程序时如何隐藏控制台窗口? - How do I hide the console window when dynamically compiling a windows application? 在Task Scheduler中运行的控制台应用程序不停止 - Console Application running in Task Scheduler without stop 如何在Windows任务计划程序启动程序时获取应用程序文件夹 - How to get application folder when program is started by Windows Task Scheduler c#console应用程序保持在任务计划程序窗口2016中运行的状态 - c# console application stays on status running in task scheduler windows 2016
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM