Web Service can't write to c:\windows\system32???

G

Guest

I'm trying to write a web service which, when run, writes a line to a file on
the web server. I've created one under IIS and the method I'm calling looks
like this...

[WebMethod]
public string HelloWorld()
{
try
{
//Server.MapPath("testfile.txt");
StreamWriter fileStream = new StreamWriter("testfile.txt", false);
fileStream.WriteLine("Testing Testing Testing!");
fileStream.Flush();
fileStream.Close();
}
catch(Exception ex)
{
return ("Error: " + ex);
}
return "Hello World";
}

But when I compile and run this in VS .NET I get the following output...

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">Error:
System.UnauthorizedAccessException: Access to the path
"C:\WINDOWS\system32\testfile.txt" is denied. at
System.IO.__Error.WinIOError(Int32 errorCode, String str) at
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean
bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize) at
System.IO.StreamWriter.CreateFile(String path, Boolean append) at
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding,
Int32 bufferSize) at System.IO.StreamWriter..ctor(String path, Boolean
append) at CPWebService.Service1.HelloWorld() in
c:\inetpub\wwwroot\cpwebservice\service1.asmx.cs:line 61</string>

Why is it trying to write to System32? And why wont it work? I've tried
setting the ASP .NET user account to Administrator and I've tried changing
all the Runtime Security Policies in the .NET Configuration in Administrative
Tools to Full Trust (yes I know how bad this is but I will sort that out
later) but I still get the same message.

Just in case it makes a difference. I'm running IIS on Windows XP
Professional with SP2.

Any ideas?

Darrell
 
J

Jon Skeet [C# MVP]

redneon said:
I'm trying to write a web service which, when run, writes a line to a file on
the web server. I've created one under IIS and the method I'm calling looks
like this...

[WebMethod]
public string HelloWorld()
{
try
{
//Server.MapPath("testfile.txt");
StreamWriter fileStream = new StreamWriter("testfile.txt", false);
fileStream.WriteLine("Testing Testing Testing!");
fileStream.Flush();
fileStream.Close();
}
catch(Exception ex)
{
return ("Error: " + ex);
}
return "Hello World";
}

But when I compile and run this in VS .NET I get the following output...

Why is it trying to write to System32?

Because that's the working directory of the process, I suspect.
And why wont it work?

Don't try to get it to work. Change where it's writing to. You don't
*really* want to write to System32, do you? That's unspeakably nasty.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top