洪星的博客(原创版,新闻除外)

信息技术 软件开发 电信 移动通信(欢迎和我交流:QQ219402,15152399197)

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  12 随笔 :: 0 文章 :: 62 评论 :: 0 引用

公告

2010年2月1日 #

谁还有更简单的请留言!部分代码方便调试,未精简。

 

VerifyIdCard.cs
1 using System;
2  using System.Text.RegularExpressions;
3
4  namespace Hongcing
5 {
6 /// <summary>
7 /// 验证公民身份号码/身份证号码升位
8 /// </summary>
9 public static class VerifyIdCard
10 {
11 static readonly int[] iW = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
12 //static readonly char[] szVerCode ={ '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
13 static readonly string szVerCode = "10X98765432";
14
15 /// <summary>
16 /// 获取校验码
17 /// </summary>
18 /// <param name="id">17位字符串</param>
19 /// <returns>返回的校验码</returns>
20 private static char GetVerifyCode(string id)
21 {
22 if (!Regex.IsMatch(id, @"^\d{17}$"))
23 throw new ArgumentException("必须是17位数字", "id");
24
25 int iS = 0;
26 for (int i = 0, j = 0; i < id.Length && j < iW.Length; i++, j++)
27 {
28 int iA = id[i] - '0';
29 iS += iA * iW[j];
30 }
31 int iY = iS % 11;
32 return szVerCode[iY];
33 }
34
35 /// <summary>
36 /// 公民身份号码 15 位升级为 18 位
37 /// </summary>
38 /// <param name="id">15 位公民身份号码</param>
39 /// <returns>18 位公民身份号码</returns>
40 public static string Upgrade(string id)
41 {
42 if (!Regex.IsMatch(id, @"^\d{15}$"))
43 throw new ArgumentException("必须是15位数字", "id");
44
45 string tempId = id.Insert(6, "19");
46 string newId = tempId + GetVerifyCode(tempId);
47 return newId;
48 }
49
50 /// <summary>
51 /// 验证 18 位公民身份号码是否有效
52 /// </summary>
53 /// <param name="id">18 位公民身份号码</param>
54 /// <returns>有效为 true,无效为 false</returns>
55 public static bool Verify(string id)
56 {
57 if (!Regex.IsMatch(id, @"^\d{17}[0-9X]$"))
58 throw new ArgumentException("必须是18位数字或者前17位为数字最后一位为大写字母 X", "id");
59
60 string tempId = id.Substring(0, 17);
61 char oldVerifyCode = id[17];
62 char newVerifyCode = GetVerifyCode(tempId);
63 return oldVerifyCode == newVerifyCode;
64 }
65 }
66 }
67

 

posted @ 2010-02-01 22:09 洪星 阅读(550) 评论(2) 编辑

MainPage.xaml
<UserControl x:Class="Hongcing.SilverlightApplication.MainPage"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Loaded="LayoutRoot_Loaded" />
</UserControl>

 

MainPage.xaml.cs
1 using System.Windows;
2  using System.Windows.Controls;
3  using System.Windows.Markup;
4
5  namespace Hongcing.SilverlightApplication
6 {
7 public partial class MainPage : UserControl
8 {
9 public MainPage()
10 {
11 InitializeComponent();
12 }
13
14 private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
15 {
16 Panel panel = sender as Panel;
17
18 //注意:
19 //XAML 内容字符串必须定义单个根元素;必须是格式良好的 XML,并且必须是有效的 XAML。
20 //根元素必须指定某一默认的 XML 命名空间。
21 //通常是 Silverlight 命名空间 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"。
22 string xaml = "<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>Hello World!</TextBlock>";
23 UIElement element = XamlReader.Load(xaml) as UIElement;
24 panel.Children.Add(element);
25 }
26 }
27 }
28

 

 在 Microsoft Visual Studio 2008 SP1 和 Silverlight 3 Tools 环境下编译通过。

posted @ 2010-02-01 21:50 洪星 阅读(600) 评论(0) 编辑

首先找到 csproj 或 vbproj 项目文件

用记事本打开

把 MSBuildToolsPath 替换为 MSBuildBinPath,只有一处,在末尾,也可以直接手工把 Tools 改为 Bin,然后直接打开项目文件,不要打开解决方案文件。

 

代码
<!--适用于 Visual Studio 2008 及后续版本-->
<!--(C# 项目)-->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!--(VB 项目)-->
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />




<!--适用于 Visual Studio 2005 及后续版本-->
<!--(C# 项目)-->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!--(VB 项目)-->
<Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />
 



<!--以上仅对以 *.cs 或 *.vb 文件为主的项目试验通过,未对包含 *.aspx、*.xaml 等项目试验-->

 

posted @ 2010-02-01 21:17 洪星 阅读(1703) 评论(2) 编辑