C# 在windows登录之前运行.exe
我是新来的,我遇到的问题是控制台应用程序。我正在搜索,我发现我可以在windows作为windows服务登录之前运行控制台应用程序,我尝试了许多过程,但我无法解决它,一些人说我需要添加代码,其他人说我只需要添加到gpedit.msc,如果有人能帮助我,请。这是我的程序代码:C# 在windows登录之前运行.exe,c#,visual-studio-2010,C#,Visual Studio 2010,我是新来的,我遇到的问题是控制台应用程序。我正在搜索,我发现我可以在windows作为windows服务登录之前运行控制台应用程序,我尝试了许多过程,但我无法解决它,一些人说我需要添加代码,其他人说我只需要添加到gpedit.msc,如果有人能帮助我,请。这是我的程序代码: public static void Main(string[] args) { try { ///Method Begins.
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服务?正确的? 在Visual Studio的专业版中,有一个用于创建Windows服务的模板。 在上,您将看到一篇关于在C#中创建基本Windows服务的文章
我认为,在Windows服务中,您将没有用于输出的控制台窗口,但您可以编写一个日志文件。您可以在系统配置启动时验证您的服务是否可用
检查一下。转到运行并键入“Msconfig”。并确保已检查要执行的应用程序。在windows登录之前,您不能运行控制台应用程序。您需要创建一个Windows服务。是否要在用户每次登录windows计算机时执行脚本?有许多方法,如编辑注册表并向其添加路径或使用组策略编辑器。看看这个类似的帖子:是的,我需要在Windows登录之前运行。。。但是我可以用我的控制台来做Windows服务,或者我需要做另一件事??你可以用你的代码来做Windows服务,你只需要创建和安装服务。Pss也是正确的,您也可以从注册表运行脚本。如果我使用Windows服务或从注册表运行脚本有什么区别?但这只有在我绕过Windows的身份验证后才起作用。。。在绕道之前我需要。@ManuelFdz你想要的是一个Windows服务。我是编程界的新手,我在工作中学习C#,我是机电工程师,语言如此不同,但我想学习不同的东西并将其混合起来。。。我认为Windows服务是我所写文章的解决方案read@ManuelFdz干杯!每个人都是第一次接触一切事物