【问题标题】:Getting System.UnauthorizedAccessException while trying to write to a file from the web service尝试从 Web 服务写入文件时获取 System.UnauthorizedAccessException
【发布时间】:2011-05-28 13:41:51
【问题描述】:

我的代码:

[WebMethod]
        public string HelloWorld()
        {
            string path = @"D:\Data\wwwroot\MyService\MyService\log.txt";
            if (File.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";
            }

        }

我得到的是: System.UnauthorizedAccessException:对路径“D:\Data\wwwroot\MyService\MyService\log.txt”的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy) 在 System.IO.FileStream..ctor(字符串路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、FileOptions 选项) 在 System.IO.StreamWriter.CreateFile(字符串路径,布尔附加) 在 System.IO.StreamWriter..ctor(字符串路径,布尔附加,编码编码,Int32 缓冲区大小) 在 System.IO.StreamWriter..ctor(字符串路径) 在 D:\Data\wwwroot\MyService\MyService\Service.asmx.cs:line 29 中的 LinkTagOutWebService.Service1.HelloWorld() 处

我需要任何安全设置吗?日志文件设置为red/write....

【问题讨论】:

    标签: web-services security file-io


    【解决方案1】:

    问题已解决,是权限问题。您必须授予应用程序正在运行的 IIS 用户写入文件的权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多