Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
windows服务文件删除c#_C# - Fatal编程技术网

windows服务文件删除c#

windows服务文件删除c#,c#,C#,windows服务示例代码 using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.IO; namespace file_delete { public partial class file_delete : ServiceBase { public file_delete() { InitializeC

windows服务示例代码

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();               
           }
如何从windows窗体调用删除文件(字符串文件夹)

您可以使用覆盖,但这只接受整数作为参数,不支持向服务传递字符串

其他选项是创建一个或用于将所需信息传递给服务并调用delete方法

编辑:回答评论中关于如何以一种非常奇怪的方式使用OnCustomCommand的问题如下

在服务中,你需要这样的东西

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);
        }
    }
}
在windows窗体应用程序中,您将有如下内容

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);
}

这不是测试,只是概念证明。同样,我不建议这样做,上面的示例只是为了说明如何在桌面应用程序和服务之间不进行这种类型的通信。

请参阅:您想设置一个频道,以便表单可以与服务“对话”。NET远程处理是最小的摩擦,WCF+命名管道可能更好。即使看起来你做得不好。我想打电话给deleteFile(file)method@Phil. (你应该把你的评论作为一个答案发布)@tgolisch Phil的评论在我写我的评论的时候击败了我,但我在一个答案中对他说了同样的话,并包括了教程的链接。OnCustomCommand是指那些介于128和256之间的。我的发送字符串可以是任何整数。。。我想如果您真的愿意,您可以执行一系列CustomCommand调用,首先以数字形式发送要执行的命令(假设为1),然后继续为文件名和路径中的每个字符调用CustomCommand,后面跟一个0,以指示您已完成将该信息传递给服务。在服务端,您可以存储此信息,并且在发送0后,使用您传递的文件名调用deleteFile。但我不建议这样做,因为它非常混乱,你为什么要这样做。试一下可能很好,只是说你已经试过了。