C# 为什么我的 .NET 应用程序在从网络驱动器运行时会崩溃?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/148879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my .NET application crash when run from a network drive?
提问by Paul Smith
My .NET application fails when run from a network drive even when the very same executable runs perfectly fine from a local hard drive?
我的 .NET 应用程序在从网络驱动器运行时失败,即使同一个可执行文件从本地硬盘驱动器运行得很好?
I tried checking for "Full trust" like so:
我试着像这样检查“完全信任”:
try
{
// Demand full trust permissions
PermissionSet fullTrust = new PermissionSet( PermissionState.Unrestricted );
fullTrust.Demand();
// Perform normal application logic
}
catch( SecurityException )
{
// Report that permissions were not full trust
MessageBox.Show( "This application requires full-trust security permissions to execute." );
}
However, this isn't helping, by which I mean the application starts up and the catch block is never entered. However, a debug build shows that the exception thrown is a SecurityException caused by an InheritanceDemand. Any ideas?
但是,这无济于事,我的意思是应用程序启动并且永远不会进入 catch 块。但是,调试版本显示抛出的异常是由 InheritanceDemand 引起的 SecurityException。有任何想法吗?
采纳答案by Davy Landman
It indeed has to do with the fact the apps on a network location are less trusted then on your local hdd (due to the default policy of the .NET framework).
这确实与网络位置上的应用程序不如本地硬盘上的应用程序受信任这一事实有关(由于 .NET 框架的默认策略)。
If I'm not mistaken Microsoft finally corrected this annoyance in .NET 3.5 SP1 (after a lot of developers complaining).
如果我没记错的话,Microsoft 终于在 .NET 3.5 SP1 中纠正了这个烦恼(在很多开发人员抱怨之后)。
I google'd it: .NET Framework 3.5 SP1 Allows managed code to be launched from a network share!
我用谷歌搜索了一下:.NET Framework 3.5 SP1 允许从网络共享启动托管代码!
回答by Gulzar Nazim
Did you try Using CasPol to Fully Trust a Share?
您是否尝试使用CasPol 来完全信任共享?
回答by TheSoftwareJedi
回答by Mats Fredriksson
This is security built in by microsoft into the .net framework. It's a way of stopping malware to be run locally with full priviliges, so you cannot change this programmatically in the code.
这是微软在 .net 框架中内置的安全性。这是一种阻止恶意软件以完全权限在本地运行的方法,因此您无法在代码中以编程方式更改此设置。
What you need to do is increase the trust of specific assemblies. You do this in the .NET Framework Configuration (Control Panel->Administrative Tools), and has to be done on each computer.
您需要做的是增加对特定程序集的信任。您可以在 .NET Framework 配置(控制面板-> 管理工具)中执行此操作,并且必须在每台计算机上执行此操作。
As with any security measures, it's a pain-in-the-ass, but will help the world to be less infected etc...
与任何安全措施一样,这是一个痛苦的过程,但将帮助世界减少感染等......
回答by Ben Hoffstein
You may have already done this, but you can use CasPol.exe to enable FullTrust for a specified network share.
您可能已经这样做了,但您可以使用 CasPol.exe 为指定的网络共享启用 FullTrust。
For example
例如
cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
CasPol.exe -m -ag 1.2 -url file:///N:/your/network/path/* FullTrust
More info here.
更多信息在这里。
回答by QuickDanger
All I had to do was mark the files Read Only (possibly unrelated) and give all permissions except Full Control to Authenticated Users. I was encountering this issue before I did that, when I had the network share only setup for Domain Users.
我所要做的就是将文件标记为只读(可能不相关)并将除完全控制之外的所有权限授予经过身份验证的用户。在我这样做之前,我遇到了这个问题,当时我只为域用户设置了网络共享。
I discovered this workaround because neither the admin shares (\server\C$) nor my own PC's shares had this problem.
我发现了这个解决方法,因为管理员共享 (\server\C$) 和我自己的 PC 共享都没有这个问题。
Edit:App is targeting .NET 3.5, no SP1 here (version 3.5.7283)
编辑:应用程序针对 .NET 3.5,此处没有 SP1(版本 3.5.7283)