Windows c#应用程序作为Windows任务调度程序
本文关键字:Windows 任务调度程序 应用程序 | 更新日期: 2023-09-27 17:51:12
我是c#编程的新手,但我不知道如何使我的应用程序作为windows任务调度程序运行。我在我的应用程序上有8个按钮,并希望在任务调度程序的特定时间执行它们。有可能……吗?请帮帮我,这是我的密码…我已经更改了ip地址。
private void button7_Click(object sender, EventArgs e)// 1)first download database to local system.
{
string _ftpURL = @"11.21.196.48"; //Host URL or address of the SFTP server
//string _ftpURL = @"11.11.11.2";
string _UserName = "add"; //User Name of the SFTP server
string _Password = "rr"; //Password of the SFTP server
int _Port = 2222; //Port No of the SFTP server (if any)
string _ftpDirectory = "/home/root/systools/WM/WebMobility.db"; //The directory in SFTP server where the files will be uploaded
string LocalDirectory = "F:''Explor''final test"; //Local directory from where the files will be uploaded
Sftp Connection = new Sftp(_ftpURL, _UserName, _Password);
Connection.Connect(_Port);
Connection.Get(_ftpDirectory, LocalDirectory);
Connection.Close();
}
private void button8_Click(object sender, EventArgs e)//2) Extracting data from DATABASE and saving it as csv.
{
SQLiteConnection m_dbConnection;
//m_dbConnection = new SQLiteConnection("Data Source= C:/Users/IT-Administrator/Desktop/WebMobility.db; Version=3;");
m_dbConnection = new SQLiteConnection("Data source = F:/Explor/final test/WebMobility.db; Version=3;");
; pwd=3xpl0rp1; Convert Zero Datetime=true;");
m_dbConnection.Open();
SQLiteCommand myCommand = new SQLiteCommand();
myCommand.Connection = m_dbConnection;
myCommand.CommandText = "select CompanyId,DateTime,Serial,ShortDeviceId,MatricolaA,Upper(Targa),CommonRoadDescription,RoadCivicNumber,GpsAddress,VerbaliVehicleTypeDescription,VehicleBrandDescription,VehicleModelDescription,CommonColorVehicleDescription,VerbaliRuleOneCode,VerbaliRuleOneDescription,VerbaliClosedNoteDescription,VerbaliRuleOnePoints,VerbaliMissedNotificationDescription from VerbaliData";
//myCommand.Connection = myConn;
DataTable data = new DataTable();
SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(myCommand);
//myAdapter.SelectCommand = myCommand;
myAdapter.Fill(data);
dataGridView1.DataSource = data;
this.dataGridView1.Refresh();
if (dataGridView1.RowCount > 0)
{
string value = "";
DataGridViewRow dr = new DataGridViewRow();
StreamWriter swOut = new StreamWriter("F:/Explor/final test/finaltest12.csv");
//write header rows to csv
for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.Write(",");
}
swOut.Write(dataGridView1.Columns[i].HeaderText);
}
swOut.WriteLine();
//write DataGridView rows to csv
for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
{
if (j > 0)
{
swOut.WriteLine();
}
dr = dataGridView1.Rows[j];
for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.Write(",");
}
value = dr.Cells[i].Value.ToString();
//replace comma's with spaces
value = value.Replace(',', ' ');
//replace embedded newlines with spaces
value = value.Replace(Environment.NewLine, " ");
swOut.Write(value);
}
}
swOut.Close();
}
m_dbConnection.Close();
}
//158.125.162.06; Database=synctest; Uid=root; Pwd=faithful
private void button1_Click(object sender, EventArgs e)// 3) upload tickets data from explor as csv to tickets table.
{
MySqlConnection conn = new MySqlConnection("Data source = 15.25.12.08; database = test;uid=rrr ;pwd=fsssss ;Convert Zero Datetime=true;");
DataTable db = new DataTable();
string strLoadData = "LOAD DATA LOCAL INFILE 'F:/Explor/final test/finaltest12.csv' INTO TABLE tickets FIELDS terminated by ',' ENCLOSED BY ''"' lines terminated by ''n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,IssueReasonCode,IssueReason,NoticeLocation,Points,Notes)";
MySqlCommand cmd1 = new MySqlCommand(strLoadData, conn);
cmd1.CommandTimeout = 6000;
cmd1.Connection = conn;
conn.Open();
cmd1.Prepare();
cmd1.ExecuteNonQuery();
conn.Close();
}
你不能从任务调度程序中调用UI Element。你可以调用一个带参数的程序,它将在没有UI的情况下执行!
首先,更改代码以提取不同方法中的所有进程,并在button_click事件中调用这些方法。
从你的代码,它是:
private void button1_Click(object sender, EventArgs e)
{
this.Process1();
}
// public is important
public void Process1()
{
MySqlConnection conn = new MySqlConnection("Data source = 15.25.12.08; database = test;uid=rrr ;pwd=fsssss ;Convert Zero Datetime=true;");
DataTable db = new DataTable();
string strLoadData = "LOAD DATA LOCAL INFILE 'F:/Explor/final test/finaltest12.csv' INTO TABLE tickets FIELDS terminated by ',' ENCLOSED BY ''"' lines terminated by ''n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,IssueReasonCode,IssueReason,NoticeLocation,Points,Notes)";
MySqlCommand cmd1 = new MySqlCommand(strLoadData, conn);
cmd1.CommandTimeout = 6000;
cmd1.Connection = conn;
conn.Open();
cmd1.Prepare();
cmd1.ExecuteNonQuery();
conn.Close();
}
对8个按钮执行此操作。
然后,在程序开始时,您可以使用参数进行切换:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
Form1 form = new Form1();
String[] arguments = Environment.GetCommandLineArgs();
if (arguments.Count() > 1)
{
Int16 valueArgument = Int16.Parse(arguments[1]);
switch(valueArgument)
{
case 1 :
form.Process1();
break;
case 2:
form.Process2();
break;
case 3:
form.Process3();
break;
case 4:
form.Process4();
break;
case 5:
form.Process5();
break;
case 6:
form.Process6();
break;
case 7:
form.Process7();
break;
case 8:
form.Process8();
break;
}
}
else
{
form.ShowDialog();
}
}
参数是你想要的进程数("1","2","3",…)。
然后,在任务调度程序中,只需使用参数"MyApp.exe 1","MyApp.exe 2"等调用您的应用程序。
如果你想有接口,只需调用"MyApp.exe"不带参数
编辑:这不是正确的方式,但这是最快的方式从你的代码。
这是因为Windows Scheduler不知道如何在Windows窗体上按按钮。通常你的应用程序会接受参数,然后基于它们做一些事情,所以:
public static void Main(string[] args)
{
// The Length property is used to obtain the length of the array.
// Notice that Length is a read-only property:
Console.WriteLine("Number of command line parameters = {0}", args.Length);
for(int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
所以,如果你调用:
myApp.exe A B C
你会得到这样的输出:
Number of command line parameters = 3
Arg[0] = [A]
Arg[1] = [B]
Arg[2] = [C]
从这里你可以这样做:
public static void Main(string[] args)
{
// The Length property is used to obtain the length of the array.
// Notice that Length is a read-only property:
if (args.Contains("Action1")
{
DoAction1();
}
if (args.Contains("Action2")
{
DoAction2();
}
}
那么你只需调用:
myApp.exe Action1
这将运行您的应用程序并执行操作1。如果您使用的是Windows窗体应用程序,您可以使用String[] arguments = Environment.GetCommandLineArgs();
来获取命令行参数并应用于上述