自定义Windows服务不会从文件夹中读取? [英] Custom windows service wont read from folder?

查看:94
本文介绍了自定义Windows服务不会从文件夹中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个在一天的特定时间激活的Windows服务,将文件夹中的一个文件复制到另一个文件夹。

文件路径:

来源: C:\\ usersrs \\bebe2011 \\desktop \\ excel

目的地:C:\\users \\bebe2011 \\\\\ \\\ newexcel

我的代码;

<前lang =c#> 受保护 覆盖 void OnStart( string [] args)
{
int hour = DateTime.Now.Hour;
int minute = DateTime.Now.Minute;
如果(小时== 11 && minute == 40
{
Copy_Excelfile_And_Paste_at_anotherloaction();
}
}

受保护 覆盖 void OnStop()
{
Create_ServiceStoptextfile();
}

public static void Copy_Excelfile_And_Paste_at_anotherloaction()
{
try
{
string source = C:\\users \\bebe2011 \ \desktop\\excels;
string 目的地= C:\ \users\\bebe2011\\desktop\\\\
ewexcel;
string filename = string .Empty;
if (!(Directory.Exists(Destination)&& Directory.Exists(source)))
返回;
string [] Templateexcelfile = Directory.GetFiles(source);
foreach 字符串文件 in Templateexcelfile)
{
if (Templateexcelfile [ 0 ]。包含(< span class =code-string>
模板))
{
filename = System.IO.Path.GetFileName (文件);
Destination = System.IO.Path.Combine(Destination,filename.Replace( 。xlsx,DateTime.Now.ToString( yyyyMMdd))+ 。xlsx);
System.IO.File.Copy(file,Destination, true );
}
}

}
catch (例外情况)
{
Create_ErrorFile(ex);
}

}





我的尝试:



我不太清楚该做什么,因为ServiceStopTextFile是在相同的文件夹中创建的,具有windows服务的状态?

解决方案

尝试调试你的应用程序,我知道服务很难。但是有可能。



将此代码放入主方法

 #if DEBUG 
服务。调试模式();
Thread.Sleep(Timeout.Infinite);
#else

ServiceBase [] ServicesToRun;
ServicesToRun = new ServiceBase []
{
service
};
ServiceBase.Run(ServicesToRun);
#endif





在您的服务中您将拥有DebugMode方法:



 public void DebugMode()
{
this.OnStart(null);
}





您可以在DEBUG模式下在VisualStudio中轻松调试服务。



这里有一个工作示例:

GitHub - xszaboj / debugWindowsService:简单的解决方案如何调试Windows服务 [ ^ ]


我怀疑该行:

  if (Templateexcelfile [ 0 ]。包含(  Template))



应该是:

  if (file.Contains( 模板))





否则,如果源文件夹中的 first 文件包含单词Template,则只会复制文件


I have created a windows service that activates at certain times of the day, to copy one file in a folder to another folder.
the filepaths:
Source: C:\\users\\bebe2011\\desktop\\excel
Destination: C:\\users\\bebe2011\\desktop\\newexcel
My code;

protected override void OnStart(string[] args)
 {
     int hour = DateTime.Now.Hour;
     int minute = DateTime.Now.Minute;
     if (hour == 11 && minute == 40)
     {
         Copy_Excelfile_And_Paste_at_anotherloaction();
     }
 }

 protected override void OnStop()
 {
     Create_ServiceStoptextfile();
 }

 public static void Copy_Excelfile_And_Paste_at_anotherloaction()
 {
     try
     {
         string source = "C:\\users\\bebe2011\\desktop\\excels";
         string Destination = "C:\\users\\bebe2011\\desktop\\newexcel";
         string filename = string.Empty;
         if (!(Directory.Exists(Destination) && Directory.Exists(source)))
             return;
         string[] Templateexcelfile = Directory.GetFiles(source);
         foreach (string file in Templateexcelfile)
         {
             if (Templateexcelfile[0].Contains("Template"))
             {
                 filename = System.IO.Path.GetFileName(file);
                 Destination = System.IO.Path.Combine(Destination, filename.Replace(".xlsx", DateTime.Now.ToString("yyyyMMdd")) + ".xlsx");
                 System.IO.File.Copy(file, Destination, true);
             }
         }

     }
     catch (Exception ex)
     {
         Create_ErrorFile(ex);
     }

 }



What I have tried:

Im not quite sure what to do , because the ServiceStopTextFile is created in same folders with the status of the windows service?

解决方案

Try to debug your application, I know it is difficult with service. But it is possible.

Put this code into your main method

#if DEBUG
service.DebugMode();
Thread.Sleep(Timeout.Infinite);
#else
           
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
{ 
    service
};
ServiceBase.Run(ServicesToRun);       
#endif



and in your service you going to have DebugMode method:

public void DebugMode()
{
     this.OnStart(null);
}



and you can easily debug your service in VisualStudio in DEBUG mode.

here is a working example:
GitHub - xszaboj/debugWindowsService: simple solution how to debug windows service[^]


I suspect the line:

if (Templateexcelfile[0].Contains("Template"))


should be:

if (file.Contains("Template"))



Otherwise, you'll only copying the files if the first file in the source folder contains the word "Template".


这篇关于自定义Windows服务不会从文件夹中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆