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 8722409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:24:45+00:00 2026-06-13T07:24:45+00:00

windows service example code using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.IO; namespace file_delete

  • 0

windows service example code

using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace file_delete
{
    public partial class file_delete : ServiceBase
    {  
        public file_delete()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {           
        }
        private void deleteFile(string folder)
        {
         System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(folder);
         System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");
           foreach (System.IO.FileInfo fi in fileNames)
           {              
               fi.Delete();               
           }

How can I call the deleteFile(string folder) from windows forms?

  • 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-13T07:24:46+00:00Added an answer on June 13, 2026 at 7:24 am

    You can use the OnCustomCommand override, but this only takes an integer as an argument and does not support passing strings to the service.

    The other options would be to create a WCF service or use Remoting to pass the information you need to the service and call the delete method.

    EDIT: to respond to a question in the comments about how to use OnCustomCommand in a very strange way is as follows.

    In the service you would need something like this.

    private const int CMD_INIT_DELETE = 1;
    private const int CMD_RUN_DELETE = 0;
    
    private bool m_CommandInit = false;
    private StringBuilder m_CommandArg = new StringBuilder();
    
    protected override void OnCustomCommand(int command)
    {
        if (command == CMD_INIT_DELETE)
        {
            this.m_CommandArg.Clear();
            this.m_CommandInit = true;
        }
        else if (this.m_CommandInit)
        {
            if (command == CMD_RUN_DELETE)
            {
                this.m_CommandInit = false;
                this.deleteFile(this.m_CommandArg.ToString());
            }
            else
            {
                this.m_CommandArg.Append((char)command);
            }
        }
    }
    

    In the windows form application you would have something like this

    private const int CMD_INIT_DELETE = 1;
    private const int CMD_RUN_DELETE = 0;
    
    private void RunServiceDeleteMethod(string delFolder)
    {
        serviceController1.ExecuteCommand(CMD_INIT_DELETE);
    
        foreach (char ch in delFolder)
            serviceController1.ExecuteCommand((int)ch);
    
        serviceController1.ExecuteCommand(CMD_RUN_DELETE);
    }
    

    THIS IS NOT TESTED and only a proof of concept. Again, I DO NOT recommend doing it this way and the above example is only to show how NOT to do this type of communications between desktop applications and services.

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

Sidebar

Related Questions

Upon installation of a Windows Service, an application is using a custom action to
I have a windows service written around some code similar to this Asynchronous Server
I'm using the Windows HTTP API to process web service requests in C++ (not
Using the method described in the MSDN for registering a Windows Service (ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createservice.htm) and
I use the Microsoft.Web.Administration.ServerManager class to manage a web site in a windows service.
@Marc Gravell gave a great example of how to install a Windows Service here
I have a working viewpager example using code I got from a tutorial. The
I've created the following: A windows service A quartz scheduler class A single IJob
I have a Windows service that writes messages to the Event Log. I also
I want to create windows service which recieves via some queue tasks to run

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.