显示日期
DateTime.Now.ToString("yyyyMMdd")
显示时间
DateTime.Now.ToString("hhmmssff")
(2)
使用枚举
enum IC_CardState{unuse=0,natural,lose,freeze,logout,BeforeDate}
if ((IC_CardType)dr_Result["CardType"]!=IC_CardType.fixCard)
{
if (System.DateTime.Now>=Convert.ToDateTime(dr_Result["EndDate"]))
{
dr_Result["AfterStatus"]=(int)IC_CardState.BeforeDate; //过期卡
}
}
以上枚举使用的的一个用例{
if (System.DateTime.Now>=Convert.ToDateTime(dr_Result["EndDate"]))
{
dr_Result["AfterStatus"]=(int)IC_CardState.BeforeDate; //过期卡
}
}
(3)
字符串换行
MessageBox.Show("aaaa" + Environment.NewLine + "bbbb");
(4)线程使用举例:
类里面定义了一个私有线程
//线程,用于接收报文
Thread t_ListenPort1;
在窗体的Load事件写入如下代码:Thread t_ListenPort1;
t_ListenPort1=new Thread(new ThreadStart(ListenPort));
t_ListenPort1.Start();
其中ListenPort的定义如下:t_ListenPort1.Start();
/// <summary>
/// 开始一个线程 接收报文 发送抱文由Click事件决定
/// </summary>
private void ListenPort()
{
//
while (true)
{
try
{
Random rdm_Message=new Random();
int i_Messagetype=rdm_Message.Next(0,2);
switch (i_Messagetype)
{
case 0:
//通行异常信息
Recive_BLCM02();
break;
case 1:
//重新验证结果
Recive_BLCR02();
break;
}
//暂停一段时间
Thread.Sleep(5000);
}
catch
{
}
}
}
可以自己来写方法Recive_BLCM02()和Recive_BLCR02()/// 开始一个线程 接收报文 发送抱文由Click事件决定
/// </summary>
private void ListenPort()
{
//
while (true)
{
try
{
Random rdm_Message=new Random();
int i_Messagetype=rdm_Message.Next(0,2);
switch (i_Messagetype)
{
case 0:
//通行异常信息
Recive_BLCM02();
break;
case 1:
//重新验证结果
Recive_BLCR02();
break;
}
//暂停一段时间
Thread.Sleep(5000);
}
catch
{
}
}
}
(5)
读取配置文件指定的信息
配置文件App.Config格式如下:
<configuration>
<!-- 错误捕捉日志-->
<configSections>
<section name="exceptionManagement" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionManagerSectionHandler,OceanSoft.DevFramework.Common" />
</configSections>
<!--mode="on"记录异常信息mode="off"不记录异常信息-->
<exceptionManagement mode="on">
<publisher assembly="OceanSoft.DevFramework.Common" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionPublisher" exclude="*" include="OceanSoft.DevFramework.Common.MyException, OceanSoft.DevFramework.Common" />
</exceptionManagement>
<startup>
<supportedRuntime version="v1.1.4322" />
<requiredRuntime version="v1.1.4322" safemode="true" />
</startup>
<appSettings>
<add key="StationProgram" value="D:\Program Files\Eclipse\eclipse.exe" />
<add key="barMainToolbar.Visible" value="True" />
<add key="FTPHost" value="172.17.100.85" />
<add key="FTPPort" value="21" />
<add key="FTPUser" value="logerp" />
<add key="FtpPass" value="logerp@oceansoft" />
<add key="SenderID" value="0003713" />
<add key="ReceiverID" value="0003714" />
<add key="BLC302Dir" value="BLC302/" />
<add key="BLC304Dir" value="BLC304/" />
<add key="BLC305Dir" value="BLC305/" />
<add key="BLC306Dir" value="BLC306/" />
<!--异常监控通道1-->
<add key="Channel1" value="0001" />
<!--异常监控通道2-->
<add key="Channel2" value="0002" />
<!--设备控制窗体刷新时间间隔 单位分钟-->
<add key="TimeInterval" value="5"/>
</appSettings>
</configuration>
首先使用引用,如下:<!-- 错误捕捉日志-->
<configSections>
<section name="exceptionManagement" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionManagerSectionHandler,OceanSoft.DevFramework.Common" />
</configSections>
<!--mode="on"记录异常信息mode="off"不记录异常信息-->
<exceptionManagement mode="on">
<publisher assembly="OceanSoft.DevFramework.Common" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionPublisher" exclude="*" include="OceanSoft.DevFramework.Common.MyException, OceanSoft.DevFramework.Common" />
</exceptionManagement>
<startup>
<supportedRuntime version="v1.1.4322" />
<requiredRuntime version="v1.1.4322" safemode="true" />
</startup>
<appSettings>
<add key="StationProgram" value="D:\Program Files\Eclipse\eclipse.exe" />
<add key="barMainToolbar.Visible" value="True" />
<add key="FTPHost" value="172.17.100.85" />
<add key="FTPPort" value="21" />
<add key="FTPUser" value="logerp" />
<add key="FtpPass" value="logerp@oceansoft" />
<add key="SenderID" value="0003713" />
<add key="ReceiverID" value="0003714" />
<add key="BLC302Dir" value="BLC302/" />
<add key="BLC304Dir" value="BLC304/" />
<add key="BLC305Dir" value="BLC305/" />
<add key="BLC306Dir" value="BLC306/" />
<!--异常监控通道1-->
<add key="Channel1" value="0001" />
<!--异常监控通道2-->
<add key="Channel2" value="0002" />
<!--设备控制窗体刷新时间间隔 单位分钟-->
<add key="TimeInterval" value="5"/>
</appSettings>
</configuration>
using System.Configuration;
读取相关配置信息如下: //根据通道编号决定显示位置
string str_channel1 = ConfigurationSettings.AppSettings["channel1"];
string str_channel2 = ConfigurationSettings.AppSettings["channel2"];
(6)string str_channel1 = ConfigurationSettings.AppSettings["channel1"];
string str_channel2 = ConfigurationSettings.AppSettings["channel2"];
根据线程当前状态关闭线程
//窗体关闭的时候关闭线程
if (t_ListenPort1.ThreadState == ThreadState.Running)
{
t_ListenPort1.Abort();
}
窗体关闭的时候线程关闭if (t_ListenPort1.ThreadState == ThreadState.Running)
{
t_ListenPort1.Abort();
}
foreach(Thread t in threadHolder.Values)
{ if(t != null && t.IsAlive)
t.Abort();
}
Form1.ActiveForm.Close();
{ if(t != null && t.IsAlive)
t.Abort();
}
Form1.ActiveForm.Close();
(7)
对已经存在的DataTable 里面新增加列
Condition pobj_Con = new Condition();
pobj_Con.Add("SQL", str_Sql);
DataTable dt = this.Search("LOGERP_ASSET_Asset_SEL_20050830124902", pobj_Con).Tables[0];
//this.Alert(dt.Rows.Count.ToString());
DataColumn ChannelState = new DataColumn();
ChannelState.DataType = System.Type.GetType("System.String");
ChannelState.ColumnName = "ChannelState";
ChannelState.DefaultValue = "";
dt.Columns.Add(ChannelState);
最重要的是最后的5行pobj_Con.Add("SQL", str_Sql);
DataTable dt = this.Search("LOGERP_ASSET_Asset_SEL_20050830124902", pobj_Con).Tables[0];
//this.Alert(dt.Rows.Count.ToString());
DataColumn ChannelState = new DataColumn();
ChannelState.DataType = System.Type.GetType("System.String");
ChannelState.ColumnName = "ChannelState";
ChannelState.DefaultValue = "";
dt.Columns.Add(ChannelState);