"应用程序试图执行安全策略不允许的操作",如何解决?
因ProgressBar的ForeColor在Windows启用了视觉样式时无效,只能在Windows经典界面下才有效,所以从网上找了一段代码来解决这个问题,但出现了在我自己的机器上正常,在别人机器上发生标题中的“应用程序试图执行安全策略不允许的操作”错误。代码如下,请帮忙解决。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestProgressBarColor
{
public partial class Form1: Form
{
[DllImport( "user32.dll", CharSet = CharSet.Auto )]
public static extern int SendMessage( int hwnd, int wMsg, int wParam, int lParam );
public const int PBM_SETBKCOLOR = 0x2001;
public const int PBM_SETBARCOLOR = 0x409;
public void SetProgressBackColor( Color c )
{/// set the back color of the bar
int a = Convert.ToInt32( c.R.ToString( ) );
int b = Convert.ToInt32( c.G.ToString( ) );
int d = Convert.ToInt32( c.B.ToString( ) );
int tot = Convert.ToInt32( ColorTranslator.ToOle( Color.FromArgb( a, b, d ) ).ToString( ) );
int j = this.progressBar1.Handle.ToInt32( );
SendMessage( j, PBM_SETBKCOLOR, 0, tot );
}
public void SetProgressForeColor( Color c )
{/// set the forecolor of the bar
int a = Convert.ToInt32( c.R.ToString( ) );
int b = Convert.ToInt32( c.G.ToString( ) );
int d = Convert.ToInt32( c.B.ToString( ) );
int tot = Convert.ToInt32( ColorTranslator.ToOle( Color.FromArgb( a, b, d ) ).ToString( ) );
int j = this.progressBar1.Handle.ToInt32( );
SendMessage( j, PBM_SETBARCOLOR, 0, tot );
}
public Form1( )
{
InitializeComponent( );
}
private void button1_Click( object sender, EventArgs e )
{
SetProgressBackColor( System.Drawing.Color.White );
SetProgressForeColor( System.Drawing.Color.LimeGreen );
this.progressBar1.Minimum = this.progressBar2.Minimum = 1;
this.progressBar1.Maximum = this.progressBar2.Maximum = 1000000;
this.progressBar1.Step = this.progressBar2.Step = 1;
this.progressBar1.Value = this.progressBar2.Value = 1;
this.progressBar2.ForeColor = System.Drawing.Color.LimeGreen;
for(int i = progressBar1.Minimum; i <= progressBar1.Maximum; i++)
{
progressBar1.PerformStep( );
progressBar2.PerformStep( );
}
}
}
} --------------------编程问答-------------------- LZ参考一下Caspol工具,也可用.NET配置工具配置安全性 --------------------编程问答-------------------- 也可以在别人的机子上打开.net DOS命令
输入
caspol -u -ag All_Code -url "该程序的目录\*" FullTrust -n MYDIR1 --------------------编程问答-------------------- 能否用代码解决?该程序要发布给用户使用的。 --------------------编程问答-------------------- 可以,写个安装类就OK了,不会叫我写吧?
获得Caspol.exe路径
获得安装路径
然后也就不用我说了吧! --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Tools.Applications.Runtime ;
using System.Windows.Forms;
using System.Text;
namespace DLSetupAction
{
[RunInstaller(true)]
public partial class DLInstaller : Installer
{
public DLInstaller()
{
InitializeComponent();
this.BeforeInstall += new InstallEventHandler(DLInstaller_BeforeInstall);
this.AfterInstall += new InstallEventHandler(DLInstaller_AfterInstall);
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
}
[DllImport("mscoree.dll")]
internal static extern void GetCORSystemDirectory([MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder buffer, int bufferLength, ref int length);
private void DLInstaller_AfterInstall(object sender, InstallEventArgs e)
{
//获得Caspol工具所在的文件路径
System.Text.StringBuilder sPath = new System.Text.StringBuilder(1024);
int size = 0;
GetCORSystemDirectory(sPath, sPath.Capacity, ref size);
sPath.Append("Caspol.exe");
//获得安装程序的目标路径
string sUrl = this.Context.Parameters["targetdir"].Trim('/');
sUrl += "*";
//设置Caspol的命令参数
StringBuilder sPar = new StringBuilder("-q -u -ag All_Code -url");
//sPar.Append(sUrl);
sPar.Append(" FullTrust");
sPar.Append(" -n");
sPar.Append(" myexcelapp");
//执行Caspol命令来配置权限
System.Diagnostics.Process.Start(sPath.ToString(), sPar.ToString());
}
private void DLInstaller_BeforeInstall(object sender, InstallEventArgs e)
{
//获得Caspol工具所在的文件路径
System.Text.StringBuilder sPath = new System.Text.StringBuilder(1024);
int size = 0;
GetCORSystemDirectory(sPath, sPath.Capacity, ref size);
sPath.Append("Caspol.exe");
//执行Caspol命令来取消权限
System.Diagnostics.Process.Start(sPath.ToString(), "-q -u -rg myexcelapp");
}
}
} --------------------编程问答-------------------- 直接把那个安装类添加到项目中,发布就可以解决权限错误吗?
补充:.NET技术 , C#