Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Running VBS script from Windows Service not working

I'm attempting to start a VBS script from a service done in C#. I've found several posts on how to do this, most of which show similar solutions, but none are working for me. I can get this method to launch, say, notepad, but not my script that I'm aware of. The script toggles Caps Lock every 10 seconds to mess with someone. When The service is running, this doesn't work, but when I launch the script from the Command Prompt it does. See code below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace WLAN_ManConfig
{
    public partial class Service1 : ServiceBase
    {
        private System.Timers.Timer timer = null;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            try
            {
                timer = new System.Timers.Timer();
                this.timer.Interval = 3000;
                this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnElapsedTime);
                timer.Enabled = true;
                string filename = "C:\\Windows\\SysWOW64\\cmd.exe";
                Process proc = new Process();
                proc.StartInfo.FileName = filename;
                proc.StartInfo.Arguments = "/C C:\\Users\\hkscribe.AD\\Documents\\CapsLock.vbs";
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();


                /*Process proc2 = new Process();
                proc2.StartInfo.FileName = "C:\\Program Files (x86)\\Notepad++\\notepad++.exe";
                //proc2.StartInfo.Arguments = "C:\\Program Files (x86)\\Notepad++\\notepad++.exe";
                proc2.Start();*/



            }
            catch (System.Exception ex)
            {
                string filename = "C:\\Windows\\SysWOW64\\cmd.exe";
                Process proc = new Process();
                proc.StartInfo.FileName = filename;
                proc.StartInfo.Arguments = "/C C:\\Users\\hkscribe.AD\\Documents\\CapsLock.vbs";
                proc.StartInfo.CreateNoWindow = false;
                proc.Start();


                /*Process proc2 = new Process();
                proc2.StartInfo.FileName = @"C:\Program Files (x86)\Notepad++\notepad++.exe";
                //proc2.StartInfo.Arguments = "C:\\Program Files (x86)\\Notepad++\\notepad++.exe";
                proc2.Start();*/
            }
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            try
            {
                /*//SendKeys.SendWait("{CAPSLOCK}");
                string filename = "C:\\Users\\hkscribe.AD\\Documents\\CapsLock.vbs";
                Process proc = new Process();
                proc.StartInfo.FileName = filename;
                //proc.StartInfo.Arguments = "/C C:\\Windows\\System32\\cscript //nologo \"" + filename + "\"" + "CapsLock.vbs";
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();*/
            }
            catch (System.Exception ex)
            {
                /*string filename = "C:\\Users\\hkscribe.AD\\Documents\\CapsLock.vbs";
                Process proc = new Process();
                proc.StartInfo.FileName = filename;
                //proc.StartInfo.Arguments = "/C C:\\Windows\\System32\\cscript //nologo \"" + filename + "\"";
                proc.StartInfo.CreateNoWindow = false;
                proc.Start();*/
            }
        }
    }
}

Comments