Có ai biết nếu có cách cài đặt dịch vụ Windows được tạo trong C# mà không cần cài đặt?Làm cách nào để cài đặt dịch vụ C# Windows mà không cần tạo trình cài đặt?
Trả lời
Bạn có thể sử dụng installutil.
Từ dòng lệnh:
installutil YourWinService.exe
Tiện ích này được cài đặt với .NET Framework
Bạn có thể thử các cửa sổ sc command
C:\WINDOWS\system32>sc create
MÔ TẢ: SC là một chương trình dòng lệnh được sử dụng cho commu rất thích thú với Bộ điều khiển và dịch vụ NT.
Tôi bao gồm lớp học cài đặt cho tôi. Tôi gọi ứng dụng bằng các tham số dòng lệnh để cài đặt hoặc gỡ cài đặt ứng dụng. Tôi cũng có trong quá khứ bao gồm một dấu nhắc cho người dùng cho dù họ muốn các dịch vụ được cài đặt khi bắt đầu trực tiếp từ dòng lệnh.
Đây là lớp tôi sử dụng:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;
namespace [your namespace here]
{
class IntegratedServiceInstaller
{
public void Install(String ServiceName, String DisplayName, String Description,
System.ServiceProcess.ServiceAccount Account,
System.ServiceProcess.ServiceStartMode StartMode)
{
System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
ProcessInstaller.Account = Account;
System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
string processPath = Process.GetCurrentProcess().MainModule.FileName;
if (processPath != null && processPath.Length > 0)
{
System.IO.FileInfo fi = new System.IO.FileInfo(processPath);
//Context = new System.Configuration.Install.InstallContext();
//Context.Parameters.Add("assemblyPath", fi.FullName);
//Context.Parameters.Add("startParameters", "Test");
String path = String.Format("/assemblypath={0}", fi.FullName);
String[] cmdline = { path };
Context = new System.Configuration.Install.InstallContext("", cmdline);
}
SINST.Context = Context;
SINST.DisplayName = DisplayName;
SINST.Description = Description;
SINST.ServiceName = ServiceName;
SINST.StartType = StartMode;
SINST.Parent = ProcessInstaller;
// http://bytes.com/forum/thread527221.html
// SINST.ServicesDependedOn = new String[] {};
System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
SINST.Install(state);
// http://www.dotnet247.com/247reference/msgs/43/219565.aspx
using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}", SINST.ServiceName), true))
{
try
{
Object sValue = oKey.GetValue("ImagePath");
oKey.SetValue("ImagePath", sValue);
}
catch (Exception Ex)
{
// System.Console.WriteLine(Ex.Message);
}
}
}
public void Uninstall(String ServiceName)
{
System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\\install.log", null);
SINST.Context = Context;
SINST.ServiceName = ServiceName;
SINST.Uninstall(null);
}
}
}
Và dưới đây là cách tôi gọi nó là:
const string serviceName = "service_name";
const string serviceTitle = "Service Title For Services Control Panel Applet";
const string serviceDescription = "A longer description of what the service does. This is used by the services control panel applet";
// Install
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Install(serviceName, serviceTitle, serviceDescription,
// System.ServiceProcess.ServiceAccount.LocalService, // this is more secure, but only available in XP and above and WS-2003 and above
System.ServiceProcess.ServiceAccount.LocalSystem, // this is required for WS-2000
System.ServiceProcess.ServiceStartMode.Automatic);
// Uninstall
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Uninstall(serviceName);
Điều này hoạt động giống như một sự quyến rũ. Rất tiện dụng. Cảm ơn vì đăng! –
- 1. cài đặt/gỡ cài đặt Dịch vụ Windows
- 2. MSDeploy để cài đặt dịch vụ windows?
- 3. Cài đặt Dịch vụ Windows theo lập trình
- 4. Trình cài đặt dịch vụ Windows không đọc tệp App.Config
- 5. Làm thế nào để cài đặt một dịch vụ windows theo lập trình trong C#?
- 6. Cài đặt Inno: Cài đặt các dịch vụ Windows bằng cách sử dụng "sc create"
- 7. Batch Script để cài đặt hoặc gỡ cài đặt Dịch vụ Windows .NET
- 8. Cách cài đặt dịch vụ Windows bằng Qt?
- 9. Cài đặt và sử dụng aspnet_merge.exe mà không cần cài đặt Windows SDK?
- 10. Cài đặt dịch vụ Windows bằng dấu nhắc lệnh Windows?
- 11. Thêm một trình cài đặt dịch vụ và trình cài đặt quy trình dịch vụ trong studio trực quan
- 12. Tôi làm cách nào để cài đặt dịch vụ cửa sổ lên máy không cài đặt Visual Studio?
- 13. npm cài đặt mà không cần ssl
- 14. Không thể tìm thấy dịch vụ windows (chỉ cần cài đặt)
- 15. Cách cài đặt python-ldap trên python 2.7 virtualenv trên windows mà không cần biên dịch
- 16. Sử dụng trình biên dịch Microsoft C++ mà không cần cài đặt Visual Studio
- 17. Làm cách nào để biên dịch mã Java trong Windows mà không cần cài đặt toàn bộ JDK?
- 18. Cài đặt Shield Limited Edition Dịch vụ WIndows
- 19. Cài đặt Dịch vụ Windows tự phát triển
- 20. dịch vụ Windows cài đặt kết thúc trong Rollback
- 21. Tạo trình cài đặt
- 22. Cài đặt maven2 mà không cần openjdk
- 23. Cài đặt gcc-C++ trên CentOS mà không cần yum
- 24. Không cài đặt 'tomcat 7' dịch vụ
- 25. chạy msbuild mà không cần cài đặt VS
- 26. API để tạo trình cài đặt trên Windows
- 27. cách tạo trình cài đặt bên trong trình cài đặt khác?
- 28. Cách thay đổi loại khởi động dịch vụ Windows trong trình cài đặt WiX
- 29. Cài đặt dịch vụ .NET bằng InstallUtil
- 30. ImageMagick Cài đặt Windows
Tôi tin rằng bạn vẫn cần phải tạo ra một trình cài đặt trong dự án của bạn cho installutil để làm việc với cấu hình của dịch vụ các thuộc tính: http://www.developer.com/net/net/article.php/11087_2173801_2 –
Dan đúng, bạn vẫn cần phải tạo trình cài đặt. Lệnh sc (xem bên dưới) sẽ cho phép bạn cài đặt/xóa/bắt đầu/dừng, vv một dịch vụ Windows mà không yêu cầu trình cài đặt (dường như, với tôi, cốt lõi của câu hỏi). Điều này rất tiện lợi vì phần lớn siêu dữ liệu dịch vụ (loại khởi động, tên tài khoản, thuộc tính khởi động lại, v.v.) được đưa vào trình cài đặt có thể được lưu trữ bên ngoài. Điều này là hữu ích gấp đôi khi kết hợp với các kịch bản để triển khai, nói MSBuild hoặc Nant, bởi vì nó không yêu cầu biên dịch lại. Nó cũng có nghĩa là bạn có thể cài đặt cho dù nó được viết bằng C#, C, C++. –
Bạn cần một lớp 'Trình cài đặt', nhưng không phải là trình cài đặt theo nghĩa là setup.exe – Nathan