423,867 Members | 3,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,867 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:l ine 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

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
redneon <re*****@discussions.microsoft.com> wrote:
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...
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.