如何在 C# 中执行以下代码行而无需在 cmd 提示符下按回车键?
How to execute the following line of code in C# without needing to hit enter in cmd prompt?
我没有使用 C# 的经验。我尝试从 .NetFramework 项目调用 .NetCore exe 文件,但在 Store_MailData(); //a method
之后执行这些行
我必须在 cmd 提示符下按回车键才能继续。
为什么它甚至不阅读以下代码?请指教:(
using System;
using System.Data.SqlClient;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace MailActivity
{
public class Program
{
[System.ComponentModel.Browsable(false)]
public System.Diagnostics.ProcessStartInfo StartInfo { get; set; }
static void Main(string[] args)
{
Store_MailData(); // a method
Console.WriteLine("Predicting intent... ");
ForeignFile.GetForeignExecutable("C:\...Debug\netcoreapp3.1\ConsoleDotNet.exe");
Console.WriteLine("Prediction effected. ");
}
public static void Store_MailData()
{
string connectString = @"Data Source="+ Environment.MachineName + ";Initial Catalog=Case_Study;Integrated Security=True";
var mails = OutlookEmails.ReadMailItems();
int i = 1;
SqlConnection con = new SqlConnection(connectString);
Console.WriteLine("Connecting to server... ");
con.Open();
foreach (var mail in mails)
{
try
{
if (con.State == System.Data.ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand("INSERT INTO tblMail(Category, Date, Sender, CC, Subject, Recipient, Attachment, Body, Status) VALUES (@Category, @Date, @Sender, @CC, @Subject, @Recipient, @Attachment, @Body, @Status)", con);
cmd.Parameters.AddWithValue("@Category", SqlDbType.VarChar).Value = 0;
cmd.Parameters.AddWithValue("@Date", SqlDbType.VarChar).Value = mail.EmailDate.ToString();
cmd.Parameters.AddWithValue("@Sender", SqlDbType.VarChar).Value = mail.EmailSender;
cmd.Parameters.AddWithValue("@CC", SqlDbType.VarChar).Value = mail.EmailCC;
cmd.Parameters.AddWithValue("@Subject", SqlDbType.VarChar).Value = mail.EmailSubject;
cmd.Parameters.AddWithValue("@Recipient", SqlDbType.VarChar).Value = mail.EmailRecipient.ToString();
cmd.Parameters.AddWithValue("@Attachment", SqlDbType.VarChar).Value = mail.EmailAttachment.ToString();
cmd.Parameters.AddWithValue("@Body", SqlDbType.VarChar).Value = mail.EmailBody;
cmd.Parameters.AddWithValue("@Status", SqlDbType.Int).Value = 0;
cmd.ExecuteNonQuery();
cmd.Dispose();
//Console.WriteLine("This serie is completed");
}
}
catch (Exception Error)
{
Console.WriteLine(Error.Message);
}
i += 1;
}
Console.WriteLine("Closing server connection... ");
con.Close();
Console.WriteLine("Total mails info sent to database: "+ (i-1));
Console.ReadLine();
}
}
}
至于 Store_MailData(),它按预期工作。它读取未读邮件、下载附件、保存到文件夹和 sql.
ForeignFile.GetForeignExecutable
(项目中的一个class)如下:
using System;
using System.Diagnostics;
namespace MailActivity
{
public class ForeignFile
{
public static void GetForeignExecutable(string methodFile)
{
try
{
using (Process myProcess = new Process())
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = methodFile;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Store_MailData
的最后一行导致程序停止,直到您按下回车键。删除:
Console.ReadLine();
你会没事的!
我没有使用 C# 的经验。我尝试从 .NetFramework 项目调用 .NetCore exe 文件,但在 Store_MailData(); //a method
之后执行这些行
我必须在 cmd 提示符下按回车键才能继续。
为什么它甚至不阅读以下代码?请指教:(
using System;
using System.Data.SqlClient;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace MailActivity
{
public class Program
{
[System.ComponentModel.Browsable(false)]
public System.Diagnostics.ProcessStartInfo StartInfo { get; set; }
static void Main(string[] args)
{
Store_MailData(); // a method
Console.WriteLine("Predicting intent... ");
ForeignFile.GetForeignExecutable("C:\...Debug\netcoreapp3.1\ConsoleDotNet.exe");
Console.WriteLine("Prediction effected. ");
}
public static void Store_MailData()
{
string connectString = @"Data Source="+ Environment.MachineName + ";Initial Catalog=Case_Study;Integrated Security=True";
var mails = OutlookEmails.ReadMailItems();
int i = 1;
SqlConnection con = new SqlConnection(connectString);
Console.WriteLine("Connecting to server... ");
con.Open();
foreach (var mail in mails)
{
try
{
if (con.State == System.Data.ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand("INSERT INTO tblMail(Category, Date, Sender, CC, Subject, Recipient, Attachment, Body, Status) VALUES (@Category, @Date, @Sender, @CC, @Subject, @Recipient, @Attachment, @Body, @Status)", con);
cmd.Parameters.AddWithValue("@Category", SqlDbType.VarChar).Value = 0;
cmd.Parameters.AddWithValue("@Date", SqlDbType.VarChar).Value = mail.EmailDate.ToString();
cmd.Parameters.AddWithValue("@Sender", SqlDbType.VarChar).Value = mail.EmailSender;
cmd.Parameters.AddWithValue("@CC", SqlDbType.VarChar).Value = mail.EmailCC;
cmd.Parameters.AddWithValue("@Subject", SqlDbType.VarChar).Value = mail.EmailSubject;
cmd.Parameters.AddWithValue("@Recipient", SqlDbType.VarChar).Value = mail.EmailRecipient.ToString();
cmd.Parameters.AddWithValue("@Attachment", SqlDbType.VarChar).Value = mail.EmailAttachment.ToString();
cmd.Parameters.AddWithValue("@Body", SqlDbType.VarChar).Value = mail.EmailBody;
cmd.Parameters.AddWithValue("@Status", SqlDbType.Int).Value = 0;
cmd.ExecuteNonQuery();
cmd.Dispose();
//Console.WriteLine("This serie is completed");
}
}
catch (Exception Error)
{
Console.WriteLine(Error.Message);
}
i += 1;
}
Console.WriteLine("Closing server connection... ");
con.Close();
Console.WriteLine("Total mails info sent to database: "+ (i-1));
Console.ReadLine();
}
}
}
至于 Store_MailData(),它按预期工作。它读取未读邮件、下载附件、保存到文件夹和 sql.
ForeignFile.GetForeignExecutable
(项目中的一个class)如下:
using System;
using System.Diagnostics;
namespace MailActivity
{
public class ForeignFile
{
public static void GetForeignExecutable(string methodFile)
{
try
{
using (Process myProcess = new Process())
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = methodFile;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Store_MailData
的最后一行导致程序停止,直到您按下回车键。删除:
Console.ReadLine();
你会没事的!