我是新來的,我有一個控制檯應用程序的問題。我正在搜索,我發現我可以在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();
}
}
在Windows登錄之前,您無法運行控制檯應用程序。您需要創建一個Windows服務。 [MSDN上的文檔相當不錯](http://msdn.microsoft.com/zh-cn/library/zt39148a(v = vs.110).aspx) –
是否每次用戶登錄時都要執行腳本Windows機器?有許多方法可以編輯註冊表併爲其添加路徑或使用組策略編輯器。看看這個類似的帖子:https://superuser.com/questions/15596/automatically-run-a-script-when-i-log-on-to-windows –
是的,我需要在Windows登錄之前運行...但我可以使用我的控制檯的Windows服務或我需要做另一件事? –