c#中的窗口服务 [英] window service in c#

查看:83
本文介绍了c#中的窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个窗口服务,将所有文件从一个文件夹移动到另一个文件夹。

我的问题是



i想移动LAN内的文件,即从一个系统到另一个系统共享文件夹。我无法做到。我怎么能这样做。

如何提供凭据以便我可以移动



以下是我的代码



i have created a window service that moves all file from one folder to other.
my problem is that

i want to move file within LAN, i.e., from one system to other system share folder. i am unable to do it. how can i do this.
how to give credentials so that i can move

below is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using System.Security.Principal;


namespace filemovet
{
    public partial class Service1 : ServiceBase
    {
        public Timer t = null;
        public Service1()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {
            t = new Timer();
            this.t.Enabled = true;
            this.t.Elapsed += new ElapsedEventHandler(t_tick);
            this.t.Interval = 10000;
        }

        protected override void OnStop()
        {
            this.t.Enabled = false;
        }
        public void t_tick(object sender, EventArgs e)
        {
            try
            {
               

               
                String dest_path = "D:\\output1";
                DirectoryInfo dirInfo = new DirectoryInfo(dest_path);
                if (dirInfo.Exists == false)
                    Directory.CreateDirectory(dest_path);
                List<string> allFiles = Directory.GetFiles("C:\\input\\", "*.*", SearchOption.AllDirectories).ToList();
                foreach (string file in allFiles)
                {
                    FileInfo mFile = new FileInfo(file);
                    if (File.Exists(dirInfo + "\\" + mFile.Name))
                    {

                        File.Delete(dirInfo + "\\" + mFile.Name);
                    }
                    // mFile.CopyTo(dirInfo + "\\" + mFile.Name);
                    mFile.MoveTo(dirInfo + "\\" + mFile.Name);
                }


            }
            catch (Exception ex)
            {

            }
        }
    }
}

推荐答案

if (File.Exists(dirInfo + "\\" + mFile.Name))
{

    File.Delete(dirInfo + "\\" + mFile.Name);
}





这不是指你删除文件吗?

然后你试图移动它....



或者我错了?



Doesn''t this mean that you delete the file?
And then you try to move it....

Or am I wrong?


你必须在通过网络访问时使用Impersonate 。

基本用法是
You have to use Impersonate while accessing over networks.
A basic usage will be
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();
try
{
    File.Copy(FilePathToBeCopiedFrom, FilePathToBeCopiedAt, true);
}
catch
{
    context.Undo();
}



但我建议您查看此链接并在服务中使用它;

C#通过需要身份验证的网络访问或复制文件 [ ^ ]



祝你好运,

OI


But i will suggest you have a look at this link and use it in your service;
C# accessing or copying files over a network that requires authentication[^]

Good luck,
OI


这篇关于c#中的窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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