我想阅读txt文件中的所有行,并使用它File.ReadAllLines。
在winforms,它工作得很好。
但是,当我尝试在webservice上做同样的事情时,它会崩溃。
System.IO.FileNotFoundException: Nieżna odnaleźćpliku 'C:\Program \MicrosoftVisualStudio9.0\Common7\IDE\nameOfFile.txt‘。
CopyOutputToDirectory设置为始终复制。
string[] lines = File.ReadAllLines("nameOfFIle.txt", Encoding.Default)文件位于webservice的webservice文件夹中,应用程序的应用程序文件夹中。
发布于 2010-02-23 10:10:49
在ASP.NET (WebForms或WebService)应用程序中,您需要使用以下内容:
string filePath = Server.MapPath(@"~/nameofFile.txt");
using (var reader = System.IO.File.OpenText(filePath))
{
... reader.ReadAllLines();
}假设文件位于WebService的根目录中。
发布于 2010-02-23 09:40:29
如果文件位于web服务的根文件夹( web.config文件所在的位置),则需要获取web服务根路径并将其与文件名组合:
var path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "nameOfFile.txt");
string[] lines = File.ReadAllLines(path, Encoding.Default);发布于 2010-02-23 09:39:43
webservice正在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\上查找文件,如错误消息中所示。
CopyOutputToDirectory会将文件复制到已编译的the服务的bin目录中。
您需要将正确的路径传递给File.ReadAllLines --如果您知道确切的位置,那么就通过它(您可以尝试HostingEnvironment.ApplicationPhysicalPath作为webservice的基本目录)。
https://stackoverflow.com/questions/2317036
复制相似问题