乱七八糟集合

用正则表达式提取指定位置的字符串值

mp3:"http://play.test.com/media/000002/......../..............01/de66a979.mp3"

regex = new Regex("mp3:"(?<mp3url>[^"]*)""); 

m = regex.Match(page);

return m.Result("${mp3url}");

 ————————————————————————————————————————————————————————————————————————————————

托管版本的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);
}

————————————————————————————————————————————————————————————————————————————————

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
uget.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).nugetNuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir).nugetNuGet.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版的问题,总之要尝鲜还是要小心啊,而且如果在工作机上出问题更是让人头痛了。

原文地址:https://www.cnblogs.com/s5689412/p/4708500.html