.NET ãç解ããï¼
ãµã¼ãã¹ã®ä½æ
.NET ã®ãµã¼ãã¹ãä½æããã«ããããè²ã 調ã¹ã¦ã¿ãã
- ServiceBase ã¯ã©ã¹ãç¶æ¿ãã¦ãµã¼ãã¹ä½ã
- ServiceController ã¯ã©ã¹ã§ .NET ã¢ããªãããµã¼ãã¹ã®éå§åæ¢ãå¶å¾¡ã§ãã
- ServiceControllerPermission ãµã¼ãã¹ ã³ã³ããã¼ã©ã«å¯¾ããã³ã¼ã ã¢ã¯ã»ã¹ ã»ãã¥ãªã㣠ã¢ã¯ã»ã¹è¨±å¯ãå¶å¾¡ã§ããããã«ãã¾ãã
- ServiceInstaller ã§ã¤ã³ã¹ãã¼ã«æã®ãµã¼ãã¹åã¨ãã表示ã¨ãããããã
- ServiceProcessInstaller ãµã¼ãã¹ãåä½ããã¨ãã®è¨å®ã¨ããã¢ã«ã¦ã³ããã¦ã¼ã¶æ¨©éãªã©ãæä½ãã¾ãã
- ServiceChangeDescription ã¿ã¼ããã«ãµã¼ãã¹ã®ã»ãã·ã§ã³å¤æ´ã®çç±ã示ãã¾ãã
- ServiceChangeReason ã¿ã¼ããã« ãµã¼ãã¹ã®ã»ãã·ã§ã³å¤æ´ã®éç¥çç±ã示ãã¾ãã
ã¨ãã¯ã©ã¹ã®æ¦è¦ã
ã¾ããªãã¨ãªãã¯ãããã«ãã¦ããä½ããªãã¦ã¯ã ãã½ã
ã¨ãããã¨ã§ãExpressEdition ã§ãã§ãããµã¼ãã¹éçºãããã
俺俺ãµã¼ãã¹ä½ã£ã¦ã¿ãã
åºæ¬çã«ããã¯ã°ã©ã¦ã³ãã§å®è¡ããããµã¼ãã¹ã£ã¦ã®ãä½ã£ã¦ã¿ãã
Windows ãµã¼ãã¹ä½ãå ´åãServiceBase ãç¶æ¿ãã¦ãé©å½ãªã¤ãã³ãã¡ã½ããããªã¼ãã©ã¤ããã¦ä½ãã
ãã®ãµã¤ããã1åããã«ã¿ã¤ãã«æ¾ã£ã¦ãã¡ã¤ã«ã«æ¸ãè¾¼ããµã¼ãã¹ãä½ãã
æå
ã« VisualStudio2008 Standard ããããï½ããã¸ã§ã¯ããã³ãã¬ãç¡ãããæ ¹æ§ã§ã©ãã«ãããã
class BlogLoader { public static string[] GetNazogengoTitles() { XElement blog = XElement.Load("http://d.hatena.ne.jp/white-azalea/rss"); XNamespace xmlns = "http://purl.org/rss/1.0/"; var titles = from p in blog.Elements() where p.Name.LocalName == "item" select p.Element(xmlns + "title").Value; return titles.ToArray(); } } public class SampleService : System.ServiceProcess.ServiceBase { System.Timers.Timer timer; static void Main(string[] args) { System.ServiceProcess.ServiceBase[] ServicesToRun = new System.ServiceProcess.ServiceBase[] { new SampleService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); Console.ReadLine(); } protected override void OnStart(string[] args) { base.OnStart(args); timer = new System.Timers.Timer(60000); timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Enabled = true; } void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { string filePath = this.Application.info.DirectoryPath.ToString() + "\\Files"; string[] results = BlogLoader.GetNazogengoTitles(); // ãã£ã¬ã¯ããªãåå¨ããã if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } // ãã¡ã¤ã«åãä½æ filePath = filePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; // ãã¡ã¤ã«ä½æã¨æ¸ã込㿠using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath)) { foreach (string single in results) { writer.WriteLine(single); } } } protected override void OnStop() { base.OnStop(); timer.Enabled = false; } }
ãµã¼ãã¹ããã»ã¹ãä½ã£ãã¨ããã§ããã®ã¾ã¾ã§ã¯ã¤ã³ã¹ãã¼ã«ãã¾ã¾ãªããªãã
ã®ã§ãæ ¹æ§ã§ãµã¼ãã¹ã¤ã³ã¹ãã¼ã©ãä½ãããã㧠InstallUtil ã§ã©ãã«ãã§ããã
namespace ServiceInstaller { using System; using System.ServiceProcess; using System.Configuration.Install; using System.Collections.Generic; [System.ComponentModel.RunInstaller(true)] public class ProjectInstaller : Installer { private System.ServiceProcess.ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller; // 以ä¸ã«ãã®ããã¸ã§ã¯ããä¾åãããµã¼ãã¹ãè¨è¿° private string[] param = new string[] { }; public ProjectInstaller() { processInstaller = new ServiceProcessInstaller(); serviceInstaller = new System.ServiceProcess.ServiceInstaller(); // Service will run under system account processInstaller.Account = ServiceAccount.LocalSystem; // Service will have Start Type of Automatic serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServicesDependedOn = this.param; serviceInstaller.ServiceName = "NazoChecker"; serviceInstaller.Description = "è¬è¨èªä½¿ãã®è¨åãé次ãã§ãã¯ãã¾ãã"; this.Installers.Add(serviceInstaller); this.Installers.Add(processInstaller); } } }
ããªãæ ¹æ§ã
ããã¦ãµã¯ãã¨ã¤ã³ã¹ãã¼ã«ï¼
PATH=%PATH%;C:\Windows\Microsoft.NET\Framework\v2.0.50727;
installutil SampleService.exe
ããããããããããï¼ï¼ï¼
ã¤ã³ã¹ãã¼ã«æ®µéã§ä¾å¤ãçºçãã¾ããã System.Security.SecurityException: ã½ã¼ã¹ãè¦ã¤ããã¾ããã§ããããããã¤ãã¾ã ã¯ãã¹ã¦ã®ãã°ãæ¤ç´¢ã§ãã¾ããã§ãããã¢ã¯ã»ã¹ä¸å¯è½ãªãã°: Security
orz |
WindowsXP ã¯ã³ã¬ã§åãã®ã§ãããWindows7/Vista é£ä¸ã¯ã»ãã¥ãªãã£ãããããããããã
cmd.exe ã管çè
権éã§åå®è¡ã
å°ãã¢ã³ã¤ã³ã¹ãã¼ã«ã¯
installutil /u SampleService.exe
Windows7 㧠this.Application.info.DirectoryPath.ToString() ã®ãã£ã¬ã¯ããªã¯ããC:\Windows\System32\ãã£ã½ãã
ãã¶ã XP ã¨ããã§ã¯ãªãããªã¼