简体   繁体   English

为什么服务中的File.ReadAllText会将其目录重置为“ C:\\ Windows \\ System32”?

[英]Why does File.ReadAllText in a service reset its directory to “C:\Windows\System32”?

I have created a service that acts also an HTTPServer, I have written html files and stored them in a folder in the same working directory, (say E:\\My_project\\Pages\\home.html ) I have a Library.cs file in E:\\My_project\\ . 我创建了一个还充当HTTPServer的服务 ,我编写了html文件并将它们存储在同一工作目录的文件夹中(例如E:\\My_project\\Pages\\home.html ),我在E:\\My_project\\有一个Library.cs文件E:\\My_project\\ In my code I have this line, 在我的代码中,我有这一行,

string content = File.ReadAllText("Pages/home.html");  

While I try to read this line, I get the following error, 当我尝试阅读此行时,出现以下错误,

mscorlib: Could not find a part of the path 'C:\WINDOWS\system32\Pages\home.html'

Earlier, it worked for some other pages, when I hardcoded the home page alone and read other pages like 404.html from those directory. 此前,当我单独对主页进行硬编码并从这些目录中读取其他页面(例如404.html)时,它可以用于其他页面。 Now that I have added the home page also to the pages folder, I get this error. 现在,我还将主页也添加到pages文件夹中,我收到此错误。

My Question is how to overcome this error and why does windows go to C:\\Windows\\System32 rather than looking in the same directory as the file. 我的问题是如何克服此错误,为什么Windows转到C:\\Windows\\System32而不是在与文件相同的目录中查找。

NOTE: Yes, I have used threading, the service uses multiple threads. 注意:是的,我使用过线程,该服务使用多个线程。

Code: 码:

Library.cs Library.cs

public static List<Route> GetRoutes() {
        List<Route> routes = new List<Route>();
        string content = File.ReadAllText("Pages/home.html");
        routes.Add(new Route
        {
            Name = "Hello Handler",
            UrlRegex = @"^/$",
            Method = "GET",
            Callable = (HttpRequest request) =>
            {
                return HttpBuilder.GetHome();
            }
        });
        return routes;
}

Service applications are like regular Windows applications in their understanding of "current directory". 服务应用程序在理解“当前目录”方面类似于常规Windows应用程序。 When you want to access a file and pass a relative name, your application tries to find the file by searching in: 1. The directory where an application is loaded. 当您要访问文件并传递相对名称时,您的应用程序将尝试通过以下方式查找文件:1.加载应用程序的目录。 2. The current directory. 2.当前目录。 3. The Windows system directory. 3. Windows系统目录。 See more details here 在这里查看更多详细信息

Now, the current directory is something specific for every process, it can be configured on startup via parameters, or at runtime using Directory.SetWorkingDirectory link . 现在,当前目录是每个进程专用的,可以在启动时通过参数进行配置,也可以在运行时使用Directory.SetWorkingDirectory link进行配置。

In you case, you need to set your working directory to where you web pages are. 在这种情况下,您需要将工作目录设置为网页所在的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#File.ReadAllText返回“NotSupportedException” - C# File.ReadAllText returning “NotSupportedException” 为什么我收到这个错误: System.UnauthorizedAccessException: &#39;C:\\Windows\\system32\\CSV_file&#39; - why to I get this error: System.UnauthorizedAccessException: 'C:\Windows\system32\CSV_file' File.ReadAllText System.IO.DirectoryNotFoundException 当目录存在并且一年内应用程序没有发生任何变化时 - File.ReadAllText System.IO.DirectoryNotFoundException when directory exists and nothing has changed with the app in a year File.ReadAllText OutOfMemoryException - File.ReadAllText OutOfMemoryException 例如File.ReadAllText()是否利用Windows文件缓存? - Does e.g. File.ReadAllText() utilize Windows File Caching? 为什么 File.ReadAllText() 也能识别 UTF-16 编码? - Why does File.ReadAllText() also recognize UTF-16 encodings? C# File.ReadAllText() 在给定目录路径时抛出“错误”异常 - 为什么? - C# File.ReadAllText() throws “wrong” exception when dir path given - why? File.ReadAllText(): System.UnauthorizedAccessException: '访问路径被拒绝。' - File.ReadAllText(): System.UnauthorizedAccessException: 'Access to the path is denied.' C#File.ReadAllText()不返回当前文件内容 - C# File.ReadAllText() not returning current file content File.ReadAllText使用CACHE - File.ReadAllText use of CACHE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM