FileSystemWatcher权限问题?

时间:2018-10-08 14:25:29

标签: c# service filesystemwatcher

我已编写了一项服务,并且正在使用wixtoolset对其进行打包以分发给客户端。当我尝试安装时,屏幕截图中显示了错误:

enter image description here

如果我将这一行注释掉

Watcher = new MyFileSystemWatcher(PathToFolder);

然后安装就可以了。这是权限问题,还是可能是原因?

这是ServiceBase类

using System.IO;
using System.ServiceProcess;


namespace DirectoryMonitoring
{
public partial class DirectoryMonitoringService : ServiceBase
{
    protected FileSystemWatcher Watcher;

    string PathToFolder = @"C:\PDFs\";

    public DirectoryMonitoringService()
    {
        Log.Instance.LogPath = @"C:\ProgramData\DirectoryMonitoring";
        Log.Instance.LogFileName = "DirectoryMonitoring";
        Watcher = new MyFileSystemWatcher(PathToFolder);
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }
}
}

这是FileSystemWatcher:

using System;
using System.IO;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Transfer;
using System.Net;

namespace DirectoryMonitoring
{
public class MyFileSystemWatcher : FileSystemWatcher
{

    private const string bucketName = "[bucketnamehidden";
    private static string filePath = "";
    private static readonly Amazon.RegionEndpoint bucketRegion = Amazon.RegionEndpoint.USEast1;
    private static IAmazonS3 s3Client;

    public  MyFileSystemWatcher()
    {
        Init();
    }

    public MyFileSystemWatcher(String inDirectoryPath):base(inDirectoryPath)
    {
        Init();
    }

    public MyFileSystemWatcher(String inDirectoryPath, string inFilter): base(inDirectoryPath, inFilter)
    {
        Init();
    }

    private void Init()
    {
        s3Client = new AmazonS3Client(bucketRegion);

        //IncludeSubDirectories = true;
        NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
        EnableRaisingEvents = true;
        Created += Watcher_Created;
        Changed += Watcher_Changed;
        Deleted += Watcher_Deleted;
        Renamed += Watcher_Renamed;
    }

    private static bool CheckForInternetConnection()
    {
        try
        {
            using (var client = new WebClient())
            using (client.OpenRead("https://clients3.google.com/generate_204"))
            {
                return true;
            }
        }
        catch
        {
            return false;
        }
    }

    public void Watcher_Created(object source, FileSystemEventArgs inArgs)
    {
        Log.WriteLine("File created or added: " + inArgs.FullPath);
        //TODO upload file to S3
        filePath = inArgs.FullPath;

        //if (CheckForInternetConnection())
            UploadFileAsync().Wait(4000);
        //else if (!CheckForInternetConnection())
            //AddToQueue(inArgs.FullPath);
    }

    public void AddToQueue(string path)
    {

    }

    public void Watcher_Changed(object source, FileSystemEventArgs inArgs)
    {
        Log.WriteLine("File changed: " + inArgs.FullPath);
        //Upload file to S3
    }

    public void Watcher_Deleted(object source, FileSystemEventArgs inArgs)
    {
        Log.WriteLine("File deleted: " + inArgs.FullPath);
    }

    public void Watcher_Renamed(object source, FileSystemEventArgs inArgs)
    {
        Log.WriteLine("File renamed: " + inArgs.FullPath);
    }

    private static async Task UploadFileAsync()
    {
        try
        {
            var fileTransferUtility = new TransferUtility(s3Client);

            //await fileTransferUtility.UploadAsync(filePath, bucketName);

            Log.WriteLine("FilePath: " + filePath);

            using (var fileToUpload = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                await fileTransferUtility.UploadAsync(fileToUpload, bucketName, fileToUpload.Name);
            }

            Log.WriteLine("File uploaded: " + filePath);
        }
        catch (AmazonS3Exception e)
        {
            Log.WriteLine("Error encountered on server. Message: '"+ e.Message +"' when writing an object");
        }
        catch (Exception e)
        {
            Log.WriteLine("Unknown encountered on server. Message: '" + e.Message + "' when writing an object");
        }
    }
}

}

有什么想法吗?如果我使用开发人员控制台cmds手动安装,它运行起来很有趣,但是wixtoolset没有成功。

Could not load file or assembly 'AWSSDK.Core, Version=3.3.0.0, 
Culture=neutral, PublicKeyToken=885c28607f98e604' or one of its 
dependencies. The system cannot find the file specified.
at DirectoryMonitoring.MyFileSystemWatcher.Init()
at DirectoryMonitoring.MyFileSystemWatcher..ctor(String 
inDirectoryPath)
at DirectoryMonitoring.DirectoryMonitoringService..ctor()
System.IO.FileNotFoundException: Could not load file or assembly 
'AWSSDK.Core, Version=3.3.0.0, Culture=neutral, 
PublicKeyToken=885c28607f98e604' or one of its dependencies. The 
system cannot find the file specified.
File name: 'AWSSDK.Core, Version=3.3.0.0, Culture=neutral, 
PublicKeyToken=885c28607f98e604'
at DirectoryMonitoring.MyFileSystemWatcher.Init()
at DirectoryMonitoring.MyFileSystemWatcher..ctor(String 
inDirectoryPath)
at DirectoryMonitoring.DirectoryMonitoringService..ctor()

=== Pre-bind state information ===
LOG: DisplayName = AWSSDK.Core, Version=3.3.0.0, Culture=neutral, 
PublicKeyToken=885c28607f98e604
(Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Darwin 
Ecosystem/Directory Monitoring/
LOG: Initial PrivatePath = NULL
Calling assembly : DirectoryMonitoring, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: AWSSDK.Core, Version=3.3.0.0, 
Culture=neutral, PublicKeyToken=885c28607f98e604
LOG: Attempting download of new URL file:///C:/Program Files 
(x86)/Darwin Ecosystem/Directory Monitoring/AWSSDK.Core.DLL.
LOG: Attempting download of new URL file:///C:/Program Files 
(x86)/Darwin Ecosystem/Directory 
Monitoring/AWSSDK.Core/AWSSDK.Core.DLL.
LOG: Attempting download of new URL file:///C:/Program Files 
(x86)/Darwin Ecosystem/Directory Monitoring/AWSSDK.Core.EXE.
LOG: Attempting download of new URL file:///C:/Program Files 
(x86)/Darwin Ecosystem/Directory 
Monitoring/AWSSDK.Core/AWSSDK.Core.EXE.

0 个答案:

没有答案