如何使用c#代码禁用IIS上的文件监视

Sac*_*hag 3 c# asp.net iis

我找到了一个代码片段,如下所示,用于禁用IIS服务器上的文件监视(文件更改通知),但代码无法按预期工作.下面的监视器对象获取NULL值.不确定是否需要更多其他代码或需要任何其他设置.任何人都可以建议为什么这可能会获得NULL值或建议是否有更好的方法在C#中执行此操作 -

//FIX disable AppDomain restart when deleting subdirectory

//This code will turn off monitoring from the root website directory.

//Monitoring of Bin, App_Themes and other folders will still be operational, so updated DLLs will still auto deploy.

System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);

object o = p.GetValue(null, null);

System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase);

object monitor = f.GetValue(o); //Returns NULL

System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); 
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 5

我在.Net 3.5 SP1上使用以下代码:

var theRuntime = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
var fcmField = typeof(HttpRuntime).GetField("_fcm", BindingFlags.NonPublic | BindingFlags.Instance);

var fcm = fcmField.GetValue(theRuntime);
fcmField.FieldType.GetMethod("Stop", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(fcm, null);
Run Code Online (Sandbox Code Playgroud)