I tried running a bat file on a network server, but when i debug it it throws an error that UNC paths are not supported, i have tried running the bat file on local system it works fine but not on network server
what should I do?
thanks in advance.
I m attaching my code below.
Service1.cs
public partial class Service1 : ServiceBase
{
private Timer timer1 = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 60000;
this.timer1.Elapsed +=new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled=true;
Library.WriteErrorLog("test windows service started");
var result = RunProcess(@"\\192.168.124.30\IT\WebCommon\webfolder" , "webupknvp.Bat", "cmd.exe", false);
if (result == 0)
{
Console.WriteLine("Sucess");
}
else
{
Console.WriteLine("failed try again");
}
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Test Service ended");
}
public void timer1_Tick(object sender, ElapsedEventArgs e)
{
}
public int RunProcess(string workDir, string appName, string args, bool hide = false)
{
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
args = "/c";
appName = "cmd.exe";
workDir = @"\\192.168.124.30\IT\WebCommon\webfolder";
proc.StartInfo.WorkingDirectory = workDir;
proc.StartInfo.FileName = appName;
proc.StartInfo.Arguments = args;
proc.StartInfo.CreateNoWindow = hide;
proc.Start();
proc.WaitForExit();
return proc.ExitCode;
}
Library.cs
public static class Library
{
public static void WriteErrorLog(Exception ex)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ":" + ex.Source.ToString().Trim() + ";" + ex.Message.ToString().Trim());
sw.Flush();
sw.Close();
}
catch
{
}
}
public static void WriteErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ":" + Message);
sw.Flush();
sw.Close();
}
catch
{
}
}
Program.cs
static void Main(String[] args)
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
if (Environment.UserInteractive)
{
if (System.Diagnostics.Debugger.IsAttached)
{
RunInteractiveServices(ServicesToRun);
}
else
{
try
{
bool hasCommands = false;
if (HasCommand(args, "install"))
{
ManagedInstallerClass.InstallHelper(new String[] { typeof(Program).Assembly.Location });
hasCommands = true;
if (HasCommand(args, "start"))
{
foreach (var service in ServicesToRun)
{
ServiceController sc = new ServiceController(service.ServiceName);
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
}
hasCommands = true;
}
}
if (HasCommand(args, "stop"))
{
foreach (var service in ServicesToRun)
{
ServiceController sc = new ServiceController(service.ServiceName);
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10000));
}
hasCommands = false;
}
if (HasCommand(args, "uninstall"))
{
ManagedInstallerClass.InstallHelper(new String[] { "/u", typeof(Program).Assembly.Location });
hasCommands = true;
}
if (!hasCommands)
{
Console.WriteLine("Usage : {0} [command] [command ...]", Environment.GetCommandLineArgs());
Console.WriteLine("Commands : ");
Console.WriteLine(" - install : Install the services");
Console.WriteLine(" - uninstall : Uninstall the services");
}
}
catch (Exception ex)
{
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error : {0}", ex.GetBaseException().Message);
Console.ForegroundColor = oldColor;
}
}
}
else
{
ServiceBase.Run(ServicesToRun);
}
}
static void RunInteractiveServices(ServiceBase[] servicesToRun)
{
Console.WriteLine();
Console.WriteLine("Start the services in interactive mode.");
Console.WriteLine();
MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ServiceBase service in servicesToRun)
{
Console.Write("Starting {0} ... ", service.ServiceName);
onStartMethod.Invoke(service, new object[] { new string[] { } });
Console.WriteLine("Started");
}
Console.WriteLine();
Console.WriteLine("Press a key to stop services and finish process...");
Console.ReadKey();
Console.WriteLine();
MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ServiceBase service in servicesToRun)
{
Console.Write("Stopping {0} ... ", service.ServiceName);
onStopMethod.Invoke(service, null);
Console.WriteLine("Stopped");
}
Console.WriteLine();
Console.WriteLine("All services are stopped.");
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine();
Console.Write("=== Press a key to quit ===");
Console.ReadKey();
}
}
static bool HasCommand(String[] args, String command)
{
if (args == null || args.Length == 0 || String.IsNullOrWhiteSpace(command)) return false;
return args.Any(a => String.Equals(a, command, StringComparison.OrdinalIgnoreCase));
}
}
}
What I have tried:
I got stuck here, no idea how to solve this.