2014-02-12 44 views
0

我是新來的,我有一個控制檯應用程序的問題。我正在搜索,我發現我可以在Windows登錄之前運行控制檯應用程序作爲Windows服務,我嘗試了很多過程,但我無法解決它,有人說我需要添加代碼,而其他人說我只需要添加gpedit.msc,請如果有人可以幫助我。這是我的程序代碼:在Windows登錄前運行.exe

public static void Main(string[] args) 
    { 

     try 
     { 

      ///Method Begins. 

      Console.WriteLine("Please wait, we are recording your access..."); 
      Console.WriteLine("Connecting with the DataBase"); 

      PCRegister.PcRegisterSoapClient cliente = new PCRegister.PcRegisterSoapClient(); 

      cliente.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["username"], ConfigurationSettings.AppSettings["password"]); 

      cliente.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; 
      cliente.ClientCredentials.Windows.AllowNtlm = true; 
      Console.WriteLine(); 

      ///This Method Gets the HOST IP 
      Console.Write("Getting your IP"); 
      Console.WriteLine(); 
      IPGlobalProperties network = IPGlobalProperties.GetIPGlobalProperties(); 
      TcpConnectionInformation[] connections = network.GetActiveTcpConnections(); 
      string ipAddress = connections[0].LocalEndPoint.Address.ToString(); 

      ///This Method Send the HostName to the DataBase and look if exist or not. 
      PCRegister.CriteriaDataSet criteriaDataSet = new PCRegister.CriteriaDataSet(); 

      PCRegister.CriteriaDataSet.CriteriaRow criteriaRow = criteriaDataSet.Criteria.NewCriteriaRow(); 

      criteriaRow.ColumnName = "HostName"; 

      criteriaRow.Value = Environment.MachineName; 

      criteriaDataSet.Criteria.AddCriteriaRow(criteriaRow); 

      criteriaDataSet.Criteria.AcceptChanges(); 

      PCRegister.PcRegisterListDataSet getList = new PCRegister.PcRegisterListDataSet(); 
       getList = cliente.GetList(criteriaDataSet); 

      Console.WriteLine("Sending Information"); 

      ///Create a New Full DataSet. 
      PCRegister.PcRegisterFullDataSet pcRegisterFullDataSet = new PCRegister.PcRegisterFullDataSet(); 

      ///Here the Get List will go to Data Base and look if are another HostName with the same name. 
      ///If Yes, this will make an update in the Data Base. 
      ///If Not, this will insert a new row. 
      Console.WriteLine("Connected");  
      if (getList.PcRegisterList.Count > 0) 
       { 
        ///Get the PrimaryKey from PcRegisterListDataset that was filled by the GetList. 
        Guid primaryKey = getList.PcRegisterList[0].ComputerID; 
        pcRegisterFullDataSet = cliente.GetByID(primaryKey); 
        Console.WriteLine(); 
        pcRegisterFullDataSet.PcRegister[0].HostName = criteriaRow.Value; 
        pcRegisterFullDataSet.PcRegister[0].IPAddress = ipAddress; 
        pcRegisterFullDataSet.PcRegister[0].ChangeDate = System.DateTime.Today; 
        Console.WriteLine("HostName:" + Environment.MachineName); 
        Console.WriteLine("Your New Host IP: " + ipAddress); 
        Console.WriteLine("Date: " + System.DateTime.Now); 
        Console.WriteLine(); 
        Console.WriteLine("You are already Registred"); 
        //Console.WriteLine("You are already registred, press ENTER to exit"); 
        cliente.Update(pcRegisterFullDataSet); 
        //Console.ReadLine(); 
       } 
       else 
       { 
        pcRegisterFullDataSet = cliente.GetNew(); 
        pcRegisterFullDataSet.PcRegister[0].HostName = criteriaRow.Value; 
        pcRegisterFullDataSet.PcRegister[0].IPAddress = ipAddress; 
        pcRegisterFullDataSet.PcRegister[0].ChangeDate = System.DateTime.Today; 
        Console.WriteLine(); 
        Console.WriteLine("HostName: " + criteriaRow.Value); 
        Console.WriteLine("Host IP: " + ipAddress); 
        Console.WriteLine("Date: " + System.DateTime.Now); 
        Console.WriteLine(); 
        Console.WriteLine("You are already Registred"); 
        //Console.WriteLine("You are already registred, press ENTER to exit"); 
        cliente.Update(pcRegisterFullDataSet); 
        //Console.ReadLine(); 
       } 
     }    

     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      Console.ReadLine(); 
     } 
    } 
+1

在Windows登錄之前,您無法運行控制檯應用程序。您需要創建一個Windows服務。 [MSDN上的文檔相當不錯](http://msdn.microsoft.com/zh-cn/library/zt39148a(v = vs.110).aspx) –

+0

是否每次用戶登錄時都要執行腳本Windows機器?有許多方法可以編輯註冊表併爲其添加路徑或使用組策略編輯器。看看這個類似的帖子:https://superuser.com/questions/15596/automatically-run-a-script-when-i-log-on-to-windows –

+0

是的,我需要在Windows登錄之前運行...但我可以使用我的控制檯的Windows服務或我需要做另一件事? –

回答

1

如果我正確理解你的問題,你不知道如何創建一個Windows服務?對? 在專業版的Visual Studio中,有一個用於創建Windows服務的模板。 在Codeproject上,您會發現一篇關於在C#中創建基本Windows服務的文章。

我認爲,在Windows服務中,您的輸出沒有控制檯窗口,但可以編寫日誌文件。

+0

我會嘗試與Windows服務,我可以這樣做的工作 –

0

您可以在系統配置啓動時驗證您的服務是否可用。

檢查。轉到RUN並輸入「Msconfig」。並確保您已經檢查了您要執行的應用程序。

+0

但是,這隻有在我繞過Windows的身份驗證後,我需要旁路。 –

+0

@ManuelFdz你要求的是Windows服務。 –

+0

我是編程界的新手,我在我的工作中學習C#,我是機電一體化工程師,語言非常不同,但我想學習不同的東西並將其混合......我認爲Windows Service是解決方案我讀的文章 –

相關問題