我的Windows服务为什么无法访问其中包含文件的文件夹? [英] How come my Windows Service cannot access a folder with files in them?

查看:204
本文介绍了我的Windows服务为什么无法访问其中包含文件的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个Console,Library和Service项目. Library项目将文件加载到文件夹中,并将文本存储在变量中.

I have setup a Console, Library, and Service project. The Library project loads up files in a folder and stores the text in variables.

然后,控制台项目将操纵这些变量.我能够成功运行Console项目,并且可以毫无问题地加载文件.这些文件在DLL中设置为始终复制",并被指定为构建操作"的内容.

The console project will then manipulate those variables. I am able to run the Console project successfully and it loads the files without a problem. The files are set to Copy Always in the DLL and are specified as Content for the Build Action.

当我尝试在服务中运行完全相同的代码时.

When I try to run the exact same code in the service.

File.ReadAllText(@"MyFolder\SomeFile.txt");

它引发以下异常.

The type initializer for 'MyLibrary.Data.ReadFiles' threw an exception.

我正在使用Setup Project类型来创建安装程序.安装该服务后,该服务的应用程序文件夹"中以及文件中确实存在文件夹MyFolder.还有什么可能导致服务无法读取这些文件的问题?

I am using the Setup Project type to create the installer. After the service installs the folder MyFolder does exist in the Application Folder of the service, as well as the files. What else could be causing my problem for the service not being able to read those files?

编辑1

public class ReadFiles {
    public static string DataFile = File.ReadAllText(@"MyFolder\SomeFile.txt");
}

推荐答案

您正在运行Windows服务的服务帐户无权读取您要访问的位置.您看到的错误与以下事实有关:您显示的代码可能存在于类型ReadFiles的构造中.

The service account that you're running the Windows service under, doesn't have rights to read from the location you're trying to access. The error you're seeing has to do with the fact that the code you showed likely exists in the construction of the type ReadFiles.

这篇关于我的Windows服务为什么无法访问其中包含文件的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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