为什么服务中的 File.ReadAllText 会将其目录重置为 "C:\Windows\System32"?
Why does File.ReadAllText in a service reset its directory to "C:\Windows\System32"?
我创建了一个 服务,它也是一个 HTTPServer,我编写了 html 文件并将它们存储在同一工作目录的文件夹中,(比如
E:\My_project\Pages\home.html
)
我在 E:\My_project\
中有一个 Library.cs 文件。在我的代码中有这一行,
string content = File.ReadAllText("Pages/home.html");
当我尝试阅读这一行时,出现以下错误,
mscorlib: Could not find a part of the path 'C:\WINDOWS\system32\Pages\home.html'
早些时候,它适用于其他一些页面,当我单独对主页进行硬编码并从这些目录中读取 404.html 等其他页面时。现在我已经将主页也添加到 pages 文件夹中,但出现此错误。
我的问题是如何克服这个错误以及为什么 windows 转到 C:\Windows\System32
而不是在与文件相同的目录中查找。
注意:是的,我使用了线程,该服务使用多个线程。
代码:
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;
}
我创建了一个 服务,它也是一个 HTTPServer,我编写了 html 文件并将它们存储在同一工作目录的文件夹中,(比如
E:\My_project\Pages\home.html
)
我在 E:\My_project\
中有一个 Library.cs 文件。在我的代码中有这一行,
string content = File.ReadAllText("Pages/home.html");
当我尝试阅读这一行时,出现以下错误,
mscorlib: Could not find a part of the path 'C:\WINDOWS\system32\Pages\home.html'
早些时候,它适用于其他一些页面,当我单独对主页进行硬编码并从这些目录中读取 404.html 等其他页面时。现在我已经将主页也添加到 pages 文件夹中,但出现此错误。
我的问题是如何克服这个错误以及为什么 windows 转到 C:\Windows\System32
而不是在与文件相同的目录中查找。
注意:是的,我使用了线程,该服务使用多个线程。
代码:
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;
}