用正则表达式提取指定位置的字符串值html
mp3:"http://play.test.com/media/000002/......../..............01/de66a979.mp3"正则表达式
regex = new Regex("mp3:\"(?<mp3url>[^\"]*)\""); 安全
m = regex.Match(page);网络
return m.Result("${mp3url}");dom
————————————————————————————————————————————————————————————————————————————————ide
托管版本的WideCharToMultiByte函数,原始的WideCharToMultiByte的PInvoke调用能够参看http://pinvoke.net/default.aspx/kernel32/WideCharToMultiByte.html函数
private int WideCharToMultiByte(String wideChar, out IntPtr multiByte)
{
multiByte = Marshal.StringToHGlobalAnsi(wideChar);工具
Int32 iNewDataLen = 0;
Byte[] byNewData = null;
bool bDefaultChar = false;
iNewDataLen = Win32.WideCharToMultiByte(Win32.CP_ACP, 0, wideChar, wideChar.Length, null, 0, IntPtr.Zero, out bDefaultChar);
byNewData = new Byte[iNewDataLen + 2];
return Win32.WideCharToMultiByte(Win32.CP_ACP, 0, wideChar, wideChar.Length, byNewData, iNewDataLen, IntPtr.Zero, out bDefaultChar);
}测试
————————————————————————————————————————————————————————————————————————————————ui
Form.PreviewKeyDown事件很差用,限制太多,若是要处理针对Form的键盘事件,能够考虑重写ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
————————————————————————————————————————————————————————————————————————————————
Tuple在Thread调用时颇有用,不须要本身再去临时声明结构来保存传递的参数,摘抄一个例子:
// Release : code02, 2009/05/29 // Author : Anytao, http://www.anytao.com public class MyRequest { public Tuple<string, Uri, DateTime> GetMyRequest() { return Tuple.Create<string, Uri, DateTime>("anytao.com", new Uri("http://anytao.net/"), DateTime.Now); } }
不过有一个问题是Tuple不能序列化,因此没办法用来保存配置信息,很遗憾。
————————————————————————————————————————————————————————————————————————————————
获取给定日期是一年中的第几周,周日做为每周的第一天,暂时只测试了2015.1.1和2015.12.27
static int WeeksInYear(DateTime date)
{
GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
return cal.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
}
————————————————————————————————————————————————————————————————————————————————
解决默认使用Random函数时产生随机数重复的问题
static int GetRandomSeed( )
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider( );
rng.GetBytes( bytes );
return BitConverter.ToInt32( bytes , 0 );
}
————————————————————————————————————————————————————————————————————————————————
要在byte[]和其余经常使用类型之间相互转换,能够用
public static short ByteToShort(byte[] b, int index = 0)
{
return BitConverter.ToInt16(b, index);
}
另外须要转换在各类值类型之间转换记得用Converter类
————————————————————————————————————————————————————————————————————————————————
项目使用NuGet包,出现下面错误的时候,若是激活了NuGet Restore以后编译仍然有如下错误提示
This project references NuGet package(s) that are missing on this computer.
能够到.csproj文件中去找到下述文本所有删除,原始文档出处:http://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore
<RestorePackages>true</RestorePackages> ... <Import Project="$(SolutionDir)\.nuget\nuget.targets" /> ... <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
————————————————————————————————————————————————————————————————————————————————
关于使用Excel组件保存Excel文件的一点经验,在两台PC上测试,一台是本身的装了Excel 2003+Office文件转换器(用来阅读和保存为高版本的Office文件的工具),一台是同事的安装了Excel 2010
m_objBook.SaveAs(outputFile,
XlFileFormat.xlWorkbookDefault,
m_objOpt,
m_objOpt, m_objOpt, m_objOpt, XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
主要的关键点在于SaveAs函数中使用的FileFormat参数,以前在网络上查到的代码片断使用的是XlFileFormat.xlXMLSpreadsheet,这样保存出来的文件后缀名在两台PC上都是xls,在Excel 2010下打开时会提示文件后缀名和格式不符
由于使用这个参数是使用OpenXML的格式来保存Excel表格文件,也就是说这里的xls文件实质上是xml文件,后缀名也应该是xml,虽然Excel能处理这样的文件,不过出于稳定性和安全性的考虑会有这样的提示,这样不是很完美
而后我测试了XlFileFormat.xlWorkbookNormal,这样保存出来的文件后缀名在两台PC上也是xls,在Excel 2010下能够正常打开,没有以前的提示,看起来算是OK了,可是细心的人仍然会发如今Excel 2010的正中标题处显示有“(兼容模式)”的字样
使用文件另存为的方式验证明际的文件格式,发现即便在Excel 2010下保存出来的文件格式仍然是97-2003的Excel格式,因此会有上述情形
最后测试了XlFileFormat.xlWorkbookDefault,这下可让不一样版本的Excel去自行选择保存的文件格式和后缀名,也就是说在Excel 2010下的时候能存为2010的格式,并且后缀名是xlsx,
在装了Excel 2003+Office文件转换器的状况下,一样会保存为2010的格式,并且后缀名是xlsx(在保存的过程当中还能看到正在转换文件的小对话框),猜想在只安装了Excel 2003的时候应该会保存为97-2003的Excel格式,后缀名为xls,不过没有实测
另外还有XlFileFormat.xlExcel9795这类的格式,能够用来显式指定文件格式,不过没有实测
————————————————————————————————————————————————————————————————————————————————
在使用.NET中的UdpClient时,若是须要指定发包的网卡,能够参考下面的连接:
http://stackoverflow.com/questions/1096142/broadcasting-udp-message-to-all-the-available-network-cards
————————————————————————————————————————————————————————————————————————————————
没法定位程序输入点EventSetInformation于动态连接库ADVAPI32.dll上
初步判断是由于安装了Visual Studio 2015 Community版引发的,EventSetInformation这个函数至少要Windows 8才支持,在Windows 7 SP1上出问题是正常的。
UPDATE: 次日开始卸载Visual Studio 2015,结果卸载以后仍然有上述问题,因而进一步卸载了.NET Framework 4.6,结果又出现了Visual Studio 2010项目没法打开的问题,错误提示和.csproj的某些配置选项有关,
因而又从新修复安装.NET Framework 4.0和Visual Studio 2010加Visual Studio 2010 SP1,仍然有问题,因而进一步修复安装Visual Studio 2013,这才正常打开2010的项目,并且开始碰到的异常也没有了。
总结:不知道是Visual Studio 2015的问题,仍是Community版的问题,总之要尝鲜仍是要当心啊,并且若是在工做机上出问题更是让人头痛了。