从映射驱动器或共享文件夹运行 .NET 程序
我编写了一个 C# Windows 窗体应用程序,用于将一台计算机上的远程文件夹(“源”文件夹是映射驱动器 -“Z:\folder”)中的文件和文件夹与另一台计算机上的另一个远程文件夹(“目标”)合并文件夹是共享文件夹的 UNC 路径 - “\\computername\sharedfolder”)。 我对这两个文件夹都有完全权限。 当我在本地计算机上运行该程序时,它工作正常,但是当我尝试从源文件夹中运行它时,它会失败并出现安全异常。
调用目标文件夹的 DirectoryInfo 构造函数(即 DirectoryInfo(@"\\computername\sharedfolder") 时发生故障。我认为问题是因为我从映射驱动器运行程序。有任何解决方法吗?
具体的例外是: 请求“System.Security.Permissions.FileIOPermission,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”类型的权限失败。
更新
好的,我将我的应用程序放入 Visual Studio 2008(之前是在 2005 年编写的),针对 .NET 3.5 框架,进行编译并再次尝试。
我得到了完全相同的错误。
更新 - 解决方案
我用.NET 3.5尝试过,但没有成功,然后我注意到你说的是3.5 SP1。 肯定需要该服务包。
问题解决了。 谢谢。
I have written a C# Windows Forms application to merge the files and folders from a remote folder on one machine ("source" folder is a mapped drive - "Z:\folder") with another remote folder on a different machine ("destination" folder is a UNC path to a shared folder - "\\computername\sharedfolder"). I have Full permissions to both folders. When I run the program on my local machine, it works fine, but when I try to run it from from within the source folder it fails with a security exception.
The failure occurs when calling the DirectoryInfo constructor for the destination folder (i.e., DirectoryInfo(@"\\computername\sharedfolder"). I assume the problem is because I am running the program from a mapped drive. Any workarounds?
The specific exception is:
Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
UPDATE
okay, I got my application into Visual Studio 2008 (it was previously coded in 2005), targeted the .NET 3.5 framework, compiled and tried again.
I got the exact same error.
UPDATE - RESOLUTION
I tried it with .NET 3.5, and it didn't work, then I noticed that you said 3.5 SP1. The service pack is definately needed.
Problem solved. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 3.5 SP1 允许通过网络共享运行应用程序。 以前的版本不允许。
.NET 3.5 SP1 allows running applications off a network share. Previous versions did not allow it.
您需要为应用程序启用 FullTrust 权限。 在网络共享上运行的 .NET 应用程序被授予本地 Intranet 安全权限,因此可以在沙箱中运行。
这是我为我们的一个在网络外运行的测试应用程序编写的批处理文件。 只需稍加修改,它就可以让您启动并运行。
如上所述,.NET 3.5 删除了此行为。
You need to enable FullTrust permissions for the application. .NET applications that run on a network share are given Local Intranet security permissions and thus run in a sandbox.
Here is a batch file that I wrote for one of our testing apps that runs off the network. It should get you up and running with minor modifications.
As stated above, .NET 3.5 removes this behaviour.