您的位置:首页 > 其它

【2013年】开发常见问题回顾(一)

2013-09-29 21:38 246 查看

【2013年】开发常见问题回顾(一)

记录开发中遇到的和别人问的较多的问题....

目录

IE10中LinkButton不可用

如何配置IIS才能通过链接下载文件

如何配置IIS通过链接是下载而不是直接打开txt/图片类型文件

C# 给虚拟目录批量添加MIME示例

C# 获取虚拟目录的物理路径示例

The name 'ScriptManager' does not exist in the current context

System.InvalidOperationException: 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序 Office 2010

Unable to find manifest signing certificate in the certificate store"

配置Session为StateServer方式

C# 用WMI获取网卡MAC地址示例代码

使用Net User命名创建帐户,如何让密码永不过期的问题

C# 得到文件头信息示例代码

在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序

C# 利用SharpZipLib对字符串进行压缩

Assembly.Load (Byte[])方法 调用内存占用一直增大的问题

IIS7/7.5配置上传大文件

项目发布在IIS中图片或CSS样式无法正常显示

Web.config文件中配置修改查询超时时间

IE10中LinkButton不可用

这应该是2013年初遇到的一个BUG,当使用Asp.Net开发Web Application时,页面使用LinkButton按钮;运行页面用IE10打开,点击LinkButton按钮出现如下图错误

没有直接弹出如下图错误,可以运行IE10开发人员工具(F12),在控制台中查看,也会输出 “__doPostBack”未定义 错误信息

展开查看示例代码
using System.DirectoryServices;
//添加导出COM组件:Active DS IIS Namespace Provider

static void Main(string[] args)
{

Console.WriteLine("正在添加已有的MIME类型...");

DirectoryEntry virDir_IKeyMgmt = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/虚拟目录名");

try
{
PropertyValueCollection mimeCollection = (PropertyValueCollection)virDir_IKeyMgmt.Properties["MimeMap"]; //获取MIME类型

string[] extArr = new string[] {".tar",
".zip",
".gz",
".gds",
".v",
".sof",
".bit",
".stp",
".edf",
".sdc",
".ucf",
".fsdb",
".vcd",
".sess",
".rc",
".result",
".jpg",
".png",
".pdf",
".doc",
".txt",
".log",
".xrc",
".lvs",
".xls",
".elf",
".tr0"};

IISOle.MimeMapClass mimeType = null;

foreach (string ext in extArr)
{
mimeType = new IISOle.MimeMapClass();
mimeType.Extension = ext;
mimeType.MimeType = "application/octet-stream";
mimeCollection.Add(mimeType);
}

virDir_IKeyMgmt.CommitChanges();//更改目录

Console.WriteLine("添加成功!");
}
catch (Exception ex)
{

Console.WriteLine("添加失败!");
Console.WriteLine("错误信息:" + ex.ToString());
}
finally
{
Console.ReadLine();
}
}

展开查看示例代码

C# 获取虚拟目录的物理路径示例

展开查看示例代码
/// <summary>
/// 获取虚拟目录的物理路径
/// </summary>
/// <param name="identifier">虚拟目录所属网站的标识符</param>
/// <param name="name">虚拟目录名称</param>
/// <returns></returns>
private string GetWebVirtualDirectoryPath(string identifier, string name)
{
DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + name);
string path = (string)de.Properties["Path"].Value;

return path;
}

/// <summary>????????
/// 获取网站的标识符
/// </summary>
/// <param name="portNumber">端口号</param>
///<returns></returns>
private string GetWebSiteIdentifier(string portNumber)
{
DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
foreach (object property in e.Properties["ServerBindings"])
{
if (property.Equals(":" + portNumber + ":"))
{
return e.Name;
}
}
}
}

//?默认为“默认网站”的标识符
return "1";
}

展开查看示例代码

The name 'ScriptManager' does not exist in the current context

错误如下图:

展开查看示例代码
static void Main(string[] args)
{
string filePath = @"文件绝对路径";

System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string bx = "";
byte buffer;
try
{
buffer = r.ReadByte();
bx = buffer.ToString();
buffer = r.ReadByte();
bx += buffer.ToString();
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
r.Close();
fs.Close();

Console.WriteLine(bx);
Console.ReadLine();
}

展开查看示例代码

使用Net User命名创建帐户,如何让密码永不过期的问题

程序通过调用net user命令创建用户,但是此命令未提供设置密码永不过期的参数,如果想设置密码永不过期,可以通过调用第三方Netuser.exe来完成。

下载地址: http://files.cnblogs.com/zhongweiv/NetUser.zip


将 netuser.exe 拷贝到 %systemroot%\system32 下。

Examples:

<Setting >:

/name: set a new name

/pwnexp:{y|n} set 'password never expires'

1. 更改用户名

netuser Administrator /name:"Admin go"

更改Administrator名字为 Admin go

netuser "John Doe" /name:DoeJ

更改John Doe名字为DoeJ

2.是用户用不过期

netuser admin /pwnexp:y

使admin 用户永不过期


net user这些操作可以通过 Network Management Functions 的User相关的方法去实现!

C# 得到文件头信息示例代码

展开查看示例代码
static void Main(string[] args)
{
string filePath = @"文件绝对路径";

System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string bx = "";
byte buffer;
try
{
buffer = r.ReadByte();
bx = buffer.ToString();
buffer = r.ReadByte();
bx += buffer.ToString();
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
r.Close();
fs.Close();

Console.WriteLine(bx);
Console.ReadLine();
}

展开查看示例代码

文件头并不是确定文件类型的准确标准,但确实能判断出一些文件,本示例其实也不能叫得到文件头的信息,只是读取了文件的前两个字节,如果作为判断文件的严谨依据,还是要根据具体文件去进去格式分析!

在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序

以IIS6为例

解决方式:

1.在IIS中网站对应的虚拟目录上右键,选属性

2.应用程序名后点创建

C# 利用SharpZipLib对字符串进行压缩

下载地址

https://github.com/icsharpcode/SharpZipLib

展开查看示例代码
#region## 压缩字符串
/// <summary>
/// 功能:压缩字符串
/// 创建人:Wilson
/// 创建时间:2012-12-27
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Compress(string str)
{
byte[] temp = System.Text.Encoding.Unicode.GetBytes(str);
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
System.IO.Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memStream);
stream.Write(temp, 0, temp.Length);
stream.Close();
byte[] compressedData = (byte[])memStream.ToArray();
return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);
}
#endregion

#region## 解压缩字符串
/// <summary>
/// 功能:解压缩字符串
/// 创建人:Wilson
/// 创建时间:2012-12-27
/// </summary>
/// <param name="compressedStr"></param>
/// <returns></returns>
public static string UnCompress(string compressedStr)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int totalLen = 0;
byte[] temp = System.Convert.FromBase64String(compressedStr); ;
byte[] writeTemp = new byte[4096];
System.IO.Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new System.IO.MemoryStream(temp));
while (true)
{
int size = stream.Read(writeTemp, 0, writeTemp.Length);
if (size > 0)
{
totalLen += size;
sb.Append(System.Text.Encoding.Unicode.GetString(writeTemp, 0, size));
}
else
{
break;
}
}
stream.Close();
return sb.ToString();
}
#endregion

展开查看示例代码

Assembly.Load (Byte[])方法 调用内存占用一直增大的问题

问题表现:动态调用WebSerivce时,因为反复调用使用了Assembly.Load (Byte[]),导致进程内存不断升高

解决方法:

//方法外声明
private static byte[] filedata = null;
private static Assembly asm = null;

//方法中使用
if (filedata == null)
{
filedata = File.ReadAllBytes(DLL_NAME);
}

if (asm == null)
{
asm = Assembly.Load(filedata);
}


IIS7/7.5配置上传大文件

在IIS7/7.5中要上传在文件,不仅需要配置

<httpRuntime executionTimeout="3600" maxRequestLength="2097151"/>


还需要配置如下requestLimits节点

<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647"/>
</requestFiltering>
</security>
</system.webServer>


PS:

executionTimeout单位:秒

maxRequestLength单位:KB

maxAllowedContentLength:bytes

maxRequestLength 表示Asp.Net允许上传的大小 默认值:4096KB 最大值:2097151KB(2G-1K)

executionTimeout 表示允许执行的最大时间 默认值是90秒 (超时只有在compilation 节点设置为时才会生效)

httpRuntime 元素(ASP.NET 设置架构)

http://msdn.microsoft.com/zh-cn/library/e1f13641(v=vs.90).aspx

system.webServer节点是IIS7引入的

不要轻易修改上传限制,以防上传大文件攻击服务器!

项目发布在IIS中图片或CSS样式无法正常显示

很多时候在开发环境中页面能正常显示,但发布在IIS中后,显示正常

最常见原因:

1.路径不对正常

特别是发布为虚拟目录时,一定要注意路径问题

2.IIS安装没有勾选“静态内容“选项

多数这种原因比较多,打开IIS配置窗口

Internet Information Services(Internet 信息服务)-->World Wild Web(万维网服务)-->Common HTTP features(常见HTTP功能)-->选中staticcontent(静态内容)

重新刷新页面即可

Web.config文件中配置修改查询超时时间

进行大数据查询或者统计数据时,常出现查询超时,通过配置Web.config连接字符串可以解决(MySQL)

Server=211.136.8.81;Port=3306;Database=lf;Uid=root;Pwd=admin123;CharSet=utf8;Pooling=True;default command timeout=3600;Connection Timeout=3600;


default command timeout和Connection Timeout从字面上就很好理解,就不解释了

友情提示:3600这个值只是示例,具体还是要配置一个相对合理的时间,资源宝贵!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: