我正在创建一个我正在创建的应用程序的问题.我试图通过我的C#应用程序启动Windows服务.当我单击我的开始按钮时,看起来一切都会通过但是当我登录到服务器时,该服务仍然没有显示它正在运行.但是,第二次运行它时,我得到一个异常,说该服务的实例已在运行.当我登录服务器时,服务似乎停止了.有没有人见过这个?
这是我的代码.
try
{
while (reader.Read())
{
int timeoutMilliseconds = 1000;
string serviceName = reader["ServiceName"].ToString();
string permission = reader["Permission"].ToString();
if (permission == "E")
{
lblServStartSuccess.Visible = true;
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
else
{
lblServErrorStart.Visible = true;
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
编辑:以下是我在一项服务上收到的例外情况:
System.InvalidOperationException:在计算机'.'上找不到服务逻辑磁盘管理器管理服务.---> System.ComponentModel.Win32Exception:指定的服务不作为已安装的服务存在---内部异常堆栈跟踪结束
我知道服务存在.我是否需要在服务前添加一些内容来告诉它要查看哪个服务器?
我在我的应用程序中收到错误,我无法弄清楚如何解决它.这是代码:
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
myConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT ServerIP FROM Servers", myConnection);
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
string serverIP = rdr["ServerIP"].ToString();
ScheduledTasks st = new ScheduledTasks(@"\\" + serverIP);
string[] taskNames = st.GetTaskNames();
foreach (string name in taskNames)
{
Task t = st.OpenTask(name);
var status = t.Status;
var recentRun = t.MostRecentRunTime;
var nextRun = t.NextRunTime;
var appName = t.ApplicationName;
SqlConnection myConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
SqlCommand myCommand2 = new SqlCommand("sp_AddScheduledTasks", myConnection2);
try
{ …Run Code Online (Sandbox Code Playgroud) 我在很多网站上看到了其中的几个主题,但我仍然遇到了问题.我已经将它添加到我的web.config的底部</configuaration>
<location path="Form.css">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
然而我的css仍然没有为所有用户加载.
这是我的web.config的表单部分
<authentication mode="Forms">
<forms name ="WebApp.ASPXAUTH"
loginUrl="login.aspx"
protection="All"
path ="/"/>
</authentication>
<authorization>
<allow users ="*"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)
还有其他我想念的东西吗?
我发现一些在线代码可以很好地用于我想做的事情.我需要一些能够加密密码,将其保存到数据库并轻松检索的东西.以下代码几乎可以满足我的一切需求.
string UserName = txtUser.Text;
string password = txtPass.Text;
string encrKey = "keyvalue";
byte[] byteKey = { };
byte[] IV = {25, 47, 60, 88, 99, 106, 125, 139};
byteKey = Encoding.UTF8.GetBytes(encrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputArray = Encoding.UTF8.GetBytes(password);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byteKey, IV), CryptoStreamMode.Write);
cs.Write(inputArray, 0, inputArray.Length);
cs.FlushFinalBlock();
password = Convert.ToBase64String(ms.ToArray());
SqlCommand cmd = new SqlCommand("INSERT INTO USers (UserName, Password) VALUES (@UserName, @Password)", myConnection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", UserName); …Run Code Online (Sandbox Code Playgroud)