说明:本文收录.NET编码的诸多有用技巧和资源,以备项目开发中查询所用。本文内容部分来源于网络,且内容将不断更新。
1、如何获得国家名
C#语言:
using System.Globalization;
using System.Collection;
RegionInfo country = new RegionInfo(new CultureInfo("en-US", false).LCID);
List<string> countryNames = new List<string>();
//To get the Country Names from the CultureInfo installed in windows
foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);
countryNames.Add(country.DisplayName.ToString());
}
//Assigning all Country names to IEnumerable
IEnumerable<string> nameAdded = countryNames.OrderBy(names =>names).Distinct();
if (!IsPostBack)
{
foreach (string item in nameAdded)
{
ddlCountry.Items.Add(item); //Adding items to DropDownList
}
}
using System.Collection;
RegionInfo country = new RegionInfo(new CultureInfo("en-US", false).LCID);
List<string> countryNames = new List<string>();
//To get the Country Names from the CultureInfo installed in windows
foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);
countryNames.Add(country.DisplayName.ToString());
}
//Assigning all Country names to IEnumerable
IEnumerable<string> nameAdded = countryNames.OrderBy(names =>names).Distinct();
if (!IsPostBack)
{
foreach (string item in nameAdded)
{
ddlCountry.Items.Add(item); //Adding items to DropDownList
}
}
2、读写INI配置文件
C#语言:
using System;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace PubOp
{
public class OperateIniFile
{
#region API函数声明
[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section,string key,
string val,string filePath);
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section,string key,
string def,StringBuilder retVal,int size,string filePath);
#endregion
#region 读Ini文件
public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
return temp.ToString();
}
else
{
return String.Empty;
}
}
#endregion
#region 写Ini文件
public static bool WriteIniData(string Section,string Key,string Value,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);
if(OpStation == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
#endregion
}
}
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace PubOp
{
public class OperateIniFile
{
#region API函数声明
[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section,string key,
string val,string filePath);
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section,string key,
string def,StringBuilder retVal,int size,string filePath);
#endregion
#region 读Ini文件
public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
return temp.ToString();
}
else
{
return String.Empty;
}
}
#endregion
#region 写Ini文件
public static bool WriteIniData(string Section,string Key,string Value,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);
if(OpStation == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
#endregion
}
}
3、将字符串转换为长整型
将输入的字符串转换为独一无二的Int64类型:
C#语言:
static Int64 GetInt64HashCode(string strText)
{
Int64 hashCode = 0;
if (!string.IsNullOrEmpty(strText))
{
//Unicode Encode Covering all characterset
byte[] byteContents = Encoding.Unicode.GetBytes(strText);
System.Security.Cryptography.SHA256 hash = new System.Security.Cryptography.
SHA256CryptoServiceProvider();
byte[] hashText = hash.ComputeHash(byteContents);
//32Byte hashText seperate
//hashCodeStart = 0~7 8Byte
//hashCodeMedium = 8~23 8Byte
//hashCodeEnd = 24~31 8Byte
//and Fold
Int64 hashCodeStart = BitConverter.ToInt64(hashText, 0);
Int64 hashCodeMedium = BitConverter.ToInt64(hashText, 8);
Int64 hashCodeEnd = BitConverter.ToInt64(hashText, 24);
hashCode = hashCodeStart ^ hashCodeMedium ^ hashCodeEnd;
}
return (hashCode);
}
{
Int64 hashCode = 0;
if (!string.IsNullOrEmpty(strText))
{
//Unicode Encode Covering all characterset
byte[] byteContents = Encoding.Unicode.GetBytes(strText);
System.Security.Cryptography.SHA256 hash = new System.Security.Cryptography.
SHA256CryptoServiceProvider();
byte[] hashText = hash.ComputeHash(byteContents);
//32Byte hashText seperate
//hashCodeStart = 0~7 8Byte
//hashCodeMedium = 8~23 8Byte
//hashCodeEnd = 24~31 8Byte
//and Fold
Int64 hashCodeStart = BitConverter.ToInt64(hashText, 0);
Int64 hashCodeMedium = BitConverter.ToInt64(hashText, 8);
Int64 hashCodeEnd = BitConverter.ToInt64(hashText, 24);
hashCode = hashCodeStart ^ hashCodeMedium ^ hashCodeEnd;
}
return (hashCode);
}
4、获得机器的IP地址
C#语言:
using System;
using System.Net;
public static class DNSHelper
{
public static string[] FetchIPs()
{
IPAddress[] addresses = FetchIpAddresses();
return Array.ConvertAll<IPAddress, string>(addresses,new Converter<IPAddress,string>(e => e.ToString()));
}
public static IPAddress[] FetchIpAddresses()
{
return Dns.GetHostAddresses(Dns.GetHostName());
}
}
using System.Net;
public static class DNSHelper
{
public static string[] FetchIPs()
{
IPAddress[] addresses = FetchIpAddresses();
return Array.ConvertAll<IPAddress, string>(addresses,new Converter<IPAddress,string>(e => e.ToString()));
}
public static IPAddress[] FetchIpAddresses()
{
return Dns.GetHostAddresses(Dns.GetHostName());
}
}
5、Url重写
一种方法是使用Request.PathInfo,而不是QueryStrings。例如:
VB.net语言:
Function GetCategory() As String
If (Request.PathInfo.Length = 0) Then
Return ""
Else
Return Request.PathInfo.Substring(1)
End If
End Function
If (Request.PathInfo.Length = 0) Then
Return ""
Else
Return Request.PathInfo.Substring(1)
End If
End Function
另外一种方法是在HttpModule中执行URL重写:
C#语言:
void Application_BeginRequest(object sender, EventArgs e) {
string fullOrigionalpath = Request.Url.ToString();
if (fullOrigionalpath.Contains("/Products/Books.aspx")) {
Context.RewritePath("/Products.aspx?Category=Books");
}
else if (fullOrigionalpath.Contains("/Products/DVDs.aspx")) {
Context.RewritePath("/Products.aspx?Category=DVDs");
}
}
string fullOrigionalpath = Request.Url.ToString();
if (fullOrigionalpath.Contains("/Products/Books.aspx")) {
Context.RewritePath("/Products.aspx?Category=Books");
}
else if (fullOrigionalpath.Contains("/Products/DVDs.aspx")) {
Context.RewritePath("/Products.aspx?Category=DVDs");
}
}
更多内容参见ScottGu的博客文章:Tip/Trick: Url Rewriting with ASP.NET