470,591 Members | 2,085 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

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

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
1 3021
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.

Similar topics

8 posts views Thread by Fabio Papa | last post: by
3 posts views Thread by Glenn Venzke | last post: by
5 posts views Thread by John | last post: by
27 posts views Thread by pisquem | last post: by
dmjpro
3 posts views Thread by dmjpro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.