Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:28:10+00:00 2026-06-13T02:28:10+00:00

I install my C# service (currently on Windows 7 ; I’ve read it exists

  • 0

I install my C# service (currently on Windows 7 ; I’ve read it exists only since XP and 2003, but it should be all I need to handle) using the ServiceProcessInstaller. My service can run under “Local System” but not “Local Service” nor “Network Service”. Even if my service is basically empty (see code below), I still get

Windows could not start the [service name] service on Local Computer.
Error 5: Access is denied.

Here is my mostly empty service:
EDIT I updated the code to give “Local Service” the necessary access rights.

// Needs System.Configuration.Install.dll and System.ServiceProcess.dll
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;

namespace ServiceTest
{
    static class Constants
    {
    public const string LocalServiceAcctName = @"NT AUTHORITY\LOCAL SERVICE";
        public const string ServiceName = "MySvc";
    }

    class Program
    {
        /// <summary>
        /// Main entry point.
        /// </summary>
        /// <param name="args">
        ///     If the 1st argument is "i", it will simply install the service.
        ///     If it's something else, it will run as a Console application.
        ///     If it's empty, it will run as a service.
        /// </param>
        static void Main(string[] args)
        {
            if (args != null && args.Length != 0 && args[0] == "i")
            {
                bool result = InstallService(Assembly.GetExecutingAssembly().Location, Constants.ServiceName);
                Console.WriteLine(result ? "INSTALLED" : "ERROR INSTALLING");
                Console.ReadLine();
            }
            else
            {
                var host = new Host();
                bool runAsSvc = args == null || args.Length == 0;
                host.Launch(runAsSvc);
            }
        }

        static bool InstallService(string exeFullPath, string serviceName)
        {
            AssemblyInstaller installer = new AssemblyInstaller(exeFullPath, null);

            if (ServiceController.GetServices().Any(svcCtlr => svcCtlr.ServiceName == serviceName))
                installer.Uninstall(null);

            Hashtable dico = new Hashtable();
            installer.Install(dico);
            installer.Commit(dico);

        // Gives "Local Service" the necessary rights on the folder and subfolders and files.
        DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(exeFullPath));
        DirectorySecurity dirSec = dirInfo.GetAccessControl(AccessControlSections.Access);

        FileSystemRights rights = FileSystemRights.Modify;
        AccessControlType accessType = AccessControlType.Allow;
        dirSec.AddAccessRule(new FileSystemAccessRule(Constants.LocalServiceAcctName, rights, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, accessType));
        dirSec.AddAccessRule(new FileSystemAccessRule(Constants.LocalServiceAcctName, rights, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, accessType));
        dirSec.AddAccessRule(new FileSystemAccessRule(Constants.LocalServiceAcctName, rights, accessType));
        dirInfo.SetAccessControl(dirSec);


            return true;
        }
    }

    class Host
    {
        internal void Launch(bool runAsSvc)
        {
            if (runAsSvc)
                RuntimeService.CreateAndRun(this);
            else
            {
                OnStart();
                Console.WriteLine("Component started as a Console app.");
                Console.WriteLine("We work a lot...and then we're done.");
                OnStop();

                Console.WriteLine("Press enter to stop.");
                Console.ReadLine();
            }
        }

        internal void OnStart() { Console.WriteLine("We're starting!"); }

        internal void OnStop() { Console.WriteLine("We're stopping..."); }
    }

    class RuntimeService : ServiceBase
    {
        Host _host;

        public RuntimeService(Host host) { _host = host; }

        protected override void OnStart(string[] args) { _host.OnStart(); }

        protected override void OnStop() { _host.OnStop(); }

        internal static void CreateAndRun(Host host) { ServiceBase.Run(new RuntimeService(host)); }
    }

    /// <summary>
    /// Class used to install the service.
    /// </summary>
    [RunInstaller(true)]
    public class RuntimeInstaller : Installer
    {
        public RuntimeInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            processInstaller.Account = ServiceAccount.LocalService; // ServiceAccount.LocalSystem;

            var serviceInstaller = new ServiceInstaller();
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = Constants.ServiceName;

            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:28:12+00:00Added an answer on June 13, 2026 at 2:28 am

    Where have you installed the service? The error seems to indicate that the Local Service account does not have access to the service location. So the service manager cannot even load the executable and get it running let alone start the service.

    I’ve frequently seen developers do this when developing/debugging a service. They install the service from one of their development folders but set it to run under another account. But by default only the user, the system and the administrators group have access to a user’s folders – so the service fails with an access denied error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The only way to install windows-service I know is using Visual Studio 2008 Command
I'm having a windows service and a custom console install that should install it
I have a windows service (currently running under Windows 2003 Server) that needs to
When trying to install my 32bit service on a Windows 64bit machine my installer
I am trying to install a windows service using MSBuild and CCNET. I am
I am trying to install a windows service written in c#/ VS2010, which uses
I m able to build a windows service and install it. I m curious
I have a simple WIX installer which will install a Windows Service. I have
Just wondering, if I install a Windows service from 64-bit process (service code embedded
.NET 4.0, Windows 7, VS 2010: Trying to install 64-bit service using the Setup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.