如何在C#中执行下面一行代码,而不需要在cmd提示符中按回车键?

问题描述 投票:0回答:1

我对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运算。

读取未读邮件,下载附件,保存到文件夹,然后进行SQL语句。ForeignFile.GetForeignExecutable 项目中的一个类)如下。

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);
            }
        }
    }
}
c# exe process.start
1个回答
2
投票

最后一行 Store_MailData 导致程序停止,直到你按下回车键。移除:

Console.ReadLine();

你就会没事了!

© www.soinside.com 2019 - 2024. All rights reserved.