1

我有一个 ASP.NET Web 服务,它将在作为参数传递给该方法的路径中创建一个文本文件。

 private void CreateFile(string path)
 {
        string strFileName = path;
        StreamWriter sw = new StreamWriter(strFileName, true);
        sw.WriteLine("");
        sw.Write("Created at " + DateTime.Now.ToString());
        sw.Close();
 }

现在我将网络中的一个文件夹作为参数传递并调用该方法

  CreateFile(@"\\192.168.0.40\\labels\\test.txt");

从 Visual Studio IDE 运行代码时,文件将在路径中创建。但是当我发布这个并部署为一个虚拟目录时,它给我带来了一些错误,比如

   "System.UnauthorizedAccessException: Access to the path '\\192.168.0.40\labels\test.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, Boolean append)

<identity impersonate="true"/>在我的web.config. 我的机器在 XP 中运行,另一台在 Windows Server 2003 中。我已将该文件夹的 WRITE 权限设置为“任何人”。

4

1 回答 1

1

您的 Web 应用程序在本地 aspnet 用户帐户下运行,该帐户无权访问网络上的资源。如果您的 IIS 版本中有应用程序池,则必须使应用程序池用户成为有权访问网络资源的域用户。如果您没有应用程序池,请在 web.config 的processModel元素中更改 web 用户。

于 2010-04-27T12:39:27.450 回答