我有一个 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 权限设置为“任何人”。