private void RAR()
{
// 把WinRAR目录复制到网站目录下。
string pathRoot = Server.MapPath("/");
string winRAR = pathRoot + "WinRAR/WinRAR.exe";
// 把Job.aspx和Default.aspx这两个文件添加到压缩包Job.rar中
string arguments = @"a Job.rar Job.aspx Default.aspx";
try
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = winRAR; // WinRaR.exe目录
info.Arguments = arguments; // 参数
info.WorkingDirectory = pathRoot;
Process p = new Process();
p.StartInfo = info;
p.Start();//启动
Response.Write("压缩成功。");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
下面代码在ASP.NET中,会因权限问题被拒绝访问。
System.DirectoryServices.DirectoryEntry directoryEntry;
directoryEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc/1");
directoryEntry.Invoke("Start", new object[] { });
错误:拒绝访问。
异常详细信息: System.UnauthorizedAccessException: 拒绝访问。
解决方法
修改web.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<identity impersonate="true" userName="administrator" password="777777"/>
</system.web>
</configuration>
在ASP.NET里面使用COM对象的时候,会被拒绝访问。
可以在web.config里面添加下面代码解决
<identity impersonate="true" userName="username" password="password"/>
public class BasePage : System.Web.UI.Page
{
public BasePage () {}
}
public class Test : BasePage
{
public Test()
{
MessageBox.Alert(text);
// 下里还会被执行
// MessageBox.Alert 中的 System.Web.HttpContext.Current.Response.End(); 不起作用
// Response.End() 在构造函数中不起作用
System.IO.File.WriteAllText("C:/1.txt", "1");
}
}
public class MessageBox
{
public static void Alert(string text)
{
System.Web.HttpContext.Current.Response.Write(text);
System.Web.HttpContext.Current.Response.End();
}
}
解决方法
不要在构造函数中执行,在 Page_Load 中执行
public class Test : System.Web.UI.Page
{
public Test()
{
// 停止了输出
System.Web.HttpContext.Current.Response.End();
// 下面的代码还会执行
System.IO.File.WriteAllText("C:/1.txt", "Test");
}
}
public class Test : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.HttpContext.Current.Response.End();
// 下面的代码不会执行
System.IO.File.WriteAllText("C:/1.txt", "Page_Load");
}
}
ASP.NET 的配置文件 web.config 中的 customErrors 元素,可以自定义处理错误信息。
<customErrors defaultRedirect="url" mode="On|Off|RemoteOnly">
<error statusCode="500" redirect="url"/>
<error statusCode="404" redirect="url"/>
</customErrors>
customErrors 元素的属性
1.defaultRedirect
发生错误时浏览器指向的默认 URL。
2.mode
1)On 启用自定义错误。
2)Off 禁用自定义错误。这允许显示详细的错误。
3)RemoteOnly 仅向远程客户端显示自定义错误,并向本地主机显示 ASP.NET 错误。
error 元素的属性
statusCode
具体的错误代码。
redirect
发生指定错误时浏览器指向的 URL。
上面当发生500错误时,会跳到一指定的页面。
这比较实用,不用设置Web服务器,就可自定义,处理错误。
经常在基类的构造函数中获取用户的信息,比如Cookie、Session等,但Session在构造时却还不存在。
不过在OnInit时就可读取Session值了。
public partial class BasePage : System.Web.UI.Page
{
public BasePage()
{
// 这里Session对象还未创建
if (System.Web.HttpContext.Current.Session == null)
{
System.Web.HttpContext.Current.Response.Write("Session Is Null");
}
else
{
System.Web.HttpContext.Current.Response.Write("Session Is Exist");
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// 这里就可以访问Session对象了
if (System.Web.HttpContext.Current.Session == null)
{
System.Web.HttpContext.Current.Response.Write("Session Is Null");
}
else
{
System.Web.HttpContext.Current.Response.Write("Session Is Exist");
}
}
}
今天突然发现Server.UrlEncode和HttpUtility.UrlEncode对字符编码输出不一样。
string str = "皮革";
Response.Write(Server.UrlEncode(str));
Response.Write("<br/>");
Response.Write(System.Web.HttpUtility.UrlEncode(str));
输出显示
%c6%a4%b8%ef
%e7%9a%ae%e9%9d%a9
在网上搜索了下,原来是编码问题。HttpUtility.UrlEncode默认是以UTF8进行编码,而Server.UrlEncode则以默认的编码进行编码。
指定编码后就一样了。
string str = "皮革";
Response.Write(Server.UrlEncode(str));
Response.Write("<br/>");
Response.Write(System.Web.HttpUtility.UrlEncode(str, Encoding.GetEncoding("GB2312")));