views:

125

answers:

4

Hi,

I wanna do read all lines from txt file and I use to it File.ReadAllLines.

At winforms it works fine.

But, when I', trying do the same at webservice at webmethod it crash

System.IO.FileNotFoundException: Nie można odnaleźć pliku 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\nameOfFile.txt'.

CopyOutputToDirectory is set to copy always.

  string[] lines = File.ReadAllLines("nameOfFIle.txt", Encoding.Default)

File is in webservice folder for webservice, and in application folder for application

A: 

The webservice is looking for the file at C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\, as indicated in the error message.

CopyOutputToDirectory would copy the file to the bin directory of the compiled webservice.

You need to pass the correct path to File.ReadAllLines - if you know the exact location, pass that through (you can try HostingEnvironment.ApplicationPhysicalPath for the base directory of the webservice).

Oded
+3  A: 

You need to obtain the web service root path and combine it with the filename if the file is in the root folder of your web service (where web.config file is located):

var path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "nameOfFile.txt");
string[] lines = File.ReadAllLines(path, Encoding.Default);
Darin Dimitrov
+1  A: 

In a ASP.NET (WebForms or WebService) application you need to use something like:

string filePath = Server.MapPath(@"~/nameofFile.txt");
using (var reader = System.IO.File.OpenText(filePath))
{
     ... reader.ReadAllLines();
}

Assuming that the file is in the root of your WebService.

Henk Holterman
no, it is in not
@phenevo, if the file is not in the root or subfolder of your app you should consider putting it there. The root (~) is your only point of reference.
Henk Holterman
A: 

Thanks for help, but it is not good fo r situation , where class which operating on this string file is in another folder.

I have folder Webservice and folder Server(there is this class) In webservice folder i have webservice project, and in server folder i have library

(this is a way which I implementing Facade pattern)

The txt file is in server folder.