C#技术杂谈

使用C#读写文件

下面的代码示意了如何将一个数据集中的数据写入文件。
try
{
DbConn.Open();
OleDbDataReader Reader = CommandLog.ExecuteReader();
try
{
StreamWriter sw = new StreamWriter(saveFile, false,
Encoding.ASCII);

while (Reader.Read() )
{

String s=Reader["IP"]+" - - ";
DateTime dt=(DateTime)Reader["VisitDate"];
//使用英美的日期格式格式化日期字符串
CultureInfo ci = new CultureInfo("en-US");

String VisitDate=dt.ToString("dd/MMM/yyyy:hh:mm:ss", ci);

s= s+ "["+VisitDate+" -0700] ";
String Ref=(String)Reader["Referer"];
//如果没有Refer,则
if (Ref.Trim()!="")
{

s=s+"/"GET / HTTP/1.1/" 200 23989 /""+Ref+"/" /""+Reader["UserAgent"]+"/"";
sw.WriteLine(s);
}

}

sw.Close();
}
finally
{
Reader.Close();
}

}
catch (Exception e)
{
Console.WriteLine("发生异常/n{0}", e.Message);
}
}

格式化字符串

string result=String.Format("Select * from TblCategory where (ParentId={0}) order by CategoryIndex", Pid);

C#Builder Open Tools Api的Bug

1. CodeDom无法获得构造函数的行号。
2. 无法获得Internal Protected 成员。
3. 无法将文件从项目中删除
4. C#Builder IOTAModuleInfo.ModuleType返回的全是空串
5. 不支持IOTASearchOptions接口。

使用反射动态设定组件属性

通过typeof(Component)获得Type对象,然后调用GetProperties获得属性列表,对于特定属性可以通过
GetProperty方法获得PropertyInfo对象,然后调用PropertyInfo对象的SetValue来设定值。示例如下:

System.Type btnType=button1.GetType();
PropertyInfo[] props=btnType.GetProperties();

foreach (PropertyInfo prop in props){
if (prop.Name=="Text")
prop.SetValue(button1, "test", null);
}

要想通知IDE组件属性的变更,加下面代码示例:

PropertyDescriptor backColorProp =
TypeDescriptor.GetProperties(Control)["BackColor"];

if (backColorProp != null) {
backColorProp.SetValue(Control, Color.Green);

也可以通过IComponentChangeService来实现通知。

对StringCollection进行排序

StringCollection类对应于Delphi中的TStringList类,但是同TStringList类相比,缺少了排序的功能,为此我写了一个方法,可以对StringCollection进行排序。

//对StringCollection进行排序
public static void Sort(StringCollection Strs,bool CaseSensitive) {
IComparer comparer=null;
if (CaseSensitive)
comparer=Comparer.DefaultInvariant;
else
comparer=CaseInsensitiveComparer.DefaultInvariant;
QuickSort(Strs, 0, Strs.Count - 1, comparer);
}

private static void QuickSort(StringCollection Strs, int L,int R, IComparer comparer) {
while (true) {
int I = L;
int J = R;
int P = (L + R) / 2;
while (true) {
while (comparer.Compare(Strs[I], Strs[P]) < 0)
I++;
while (comparer.Compare(Strs[J], Strs[P]) > 0)
J--;
if (I <= J) {
ExchangeStrings(Strs, I, J);
if (P == I)
P = J;
else if (P == J)
P = I;
I++;
J--;
}
if (I > J)
break;
}
if (L < J)
QuickSort(Strs, L, J, comparer);
L = I;
if (I >= R)
break;
}
}

//交换字符串的位置
public static void ExchangeStrings(StringCollection Strs, int I, int J ) {
string si=Strs[I];
string sj=Strs[J];
Strs.RemoveAt(I);
Strs.Insert(I,sj);
Strs.RemoveAt(J);
Strs.Insert(J,si);
}

从Assemble中加载自定义资源

用Resourcer向Resx文件中添加图标、位图或者字符串等资源后,调用下面示例代码就可以加载资源了:

//加载资源
ResourceManager rm =
new ResourceManager("SharpPlus.Resources", Assembly.GetExecutingAssembly());
Icon LogoIcon=(Icon)rm.GetObject("Logo.ico");

将字符串复制到剪贴板

Clipboard.SetDataObject("Test");

获取程序文件的版本信息

//获得运行时的Assembly的版本信息
public static string GetAssemblyVersion()
{
Assembly myAssembly =Assembly.GetExecutingAssembly();
FileVersionInfo myFileVersion =FileVersionInfo.GetVersionInfo(myAssembly.Location);
return string.Format("{0}.{1}.{2}",myFileVersion.FileMajorPart, myFileVersion.FileMinorPart, myFileVersion.FileBuildPart);
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值