Getting System.UnauthorizedAccessException while trying to write to a file from the web service
My code:
[WebMethod]
public string HelloWorld()
{
string path = @"D:\Data\wwwroot\MyService\MyService\log.txt";
if (Fil开发者_如何学运维e.Exists(path))
{
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write("Some sample text for the file");
return "wrote to a file";
}
}
else
{
return "file doesnt exist";
}
}
what I am getting is: System.UnauthorizedAccessException: Access to the path 'D:\Data\wwwroot\MyService\MyService\log.txt' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) 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) at LinkTagOutWebService.Service1.HelloWorld() in D:\Data\wwwroot\MyService\MyService\Service.asmx.cs:line 29
Any security settings I need? The log file is set to red/write....
Issue solved, it was a permissions issue. You have to grant the IIS user that your app is running under the permission to write to the file.
精彩评论