1. 读写文件操作

using (file = new System.IO.StreamReader(inputfile))
{
  using (outfile = new System.IO.StreamWriter(outputfile))
  {      
    try
    {
      while ((line = file.ReadLine()) != null)
      {
        /// do something
      }
    }
    catch (Exception e)
    {
      ...
    }
  }
} 2. 文件及文件夹的相关API 得到文件当前目录:  string fileDir = System.IO.Path.GetDirectoryName(fileName);
得到当前文件的名称   System.IO.Path.GetFileNameWithoutExtension(fileName)
判断文件是否存在:  System.IO.File.Exists(fileName)
判断文件夹是否存在: Directory.Exists(directory)
删除文件夹及子目录: Directory.Delete(directory, true);
新建文件夹:   Directory.CreateDirectory(directory); 得到当前文件夹的所有子文件夹: System.IO.Directory.GetDirectories(directory)
得到当前文件夹下所有文件: System.IO.Directory.GetFiles(directory)
得到文件夹信息:    DirectoryInfo di = new DirectoryInfo(fileDir);
文件夹的名字:    di.Name
文件夹的上级文件夹名字: di.Parent.Name
得到文件的相关信息: FileInfo fileInfo = new FileInfo(fileName);
得到文件大小: fileInfo.Length
得到文件最后修改时间:fileInfo.LastWriteTimeUtc 3. 多线程的lock
public class TestMonitor
{
  /// <summary>
  /// lock
  /// </summary>
  private readonly object mlock = new object();
  
  /// <summary>
  /// test lock
  /// </summary>
  public void TestStatus()
  {
    bool taken = false;
    try
    {
      Monitor.Enter(mlock, ref taken);
      /// do something
    }
    finally
    {
      if (taken == true)
      {
        Monitor.Exit(mlock);
      }
    }
  }
} 4. 程序中配置一个计时器类(Monitor),用于做运行时间测算:
this.beginTime = DateTime.UtcNow;
this.endTime = DateTime.UtcNow; 

this.duration = this.endTimeSpan.Subtract(this.beginTime);
5. 程序中配置一个日志类(Logger),用于记录程序运行异常及输出,而且最好可以把Logger类设置成统一接口,用于console和文本日志的输出
Logger.WriteLine(string, msgTag);

6. 字符串常量定义在前面,const或readonly
7. 项目中要有一个common settings 类,用于设置程序的各种参数值,Application.Properties.Settings.Default.xxx

8. SqlClient数据库总结:

1. Insert:
try
{
  using (SqlConnection conn = new SqlConnection(this.connectionString))
  {
    using (SqlCommand cmd = conn.CreateCommand())
    {
      conn.Open();
      cmd.CommandText = @"insert table values (@Test);";
      cmd.Parameters.AddWithValue("@Test", "testData");
      cmd.ExecuteNonQuery();         
    }   
  }
}
catch (Exception e)
{
  throw e;
} 2. Build Connection String:
SqlConnectionStringBuilder connStringBuilder;
connStringBuilder = new SqlConnectionStringBuilder();
connStringBuilder.DataSource = this.dataSource;
connStringBuilder.InitialCatalog = this.databaseName;
connStringBuilder.Encrypt = true;
connStringBuilder.TrustServerCertificate = false;
connStringBuilder.UserID = this.userName;
connStringBuilder.Password = this.password;
this.connectionString = connStringBuilder.ToString();
3. Query:
SqlDataReader reader;
DataTable result = new DataTable(); try
{
  using (SqlConnection conn = new SqlConnection(this.connectionString))
  {
    using (SqlCommand cmd = conn.CreateCommand())
    {
      conn.Open();
      SqlCommand command = new SqlCommand(sqlcommand, conn);
      reader = command.ExecuteReader();
      result.Load(reader);
    }
  }
}
catch (Exception e)
{
  throw e;
}
4. 最好建立一个通用的接口,可以给不同的应用使用同一个接口,比如insert的接口,需要统一。


 









       

近期C#项目中总结的更多相关文章

  1. 《AndroidStudio每日一贴》2.高速查看项目中近期的变更

    <AndroidStudio每日一贴>2.高速查看项目中近期的变更 高速查看项目中近期的变更,使用快捷键: option + shift +c 很多其它有用技巧请查看<Android ...

  2. javaWeb项目中Web.xml的基本配置

    这个地址写的非常好 ,欢迎大家访问 Å:http://www.cnblogs.com/hxsyl/p/3435412.html 一.理论准备 先说下我记得xml规则,必须有且只有一个根节点,大小写敏感 ...

  3. JavaWeb 项目中的绝对路径和相对路径以及问题的解决方式

    近期在做JavaWeb项目,总是出现各种的路径错误,并且发现不同情况下 /  所代表的含义不同,导致在调试路径上浪费了大量时间. 在JavaWeb项目中尽量使用绝对路径  由于使用绝对路径是绝对不会出 ...

  4. android 项目中使用对话框统一封装

    近期在做拼车项目中使用到了一些对话框,而且在非常多地方都使用到了,既然非常多地方使用到,那么肯定要封装一下,

  5. ios -Unity3D的EasyAR集成到已经有项目中。

    近期 在做AR这一块,用EasyAR集成到iOS端,由于现在到项目已经上线,下一版本要做一个AR功能,于是迫于需求需要,自己研究和翻阅读好多集成到资料. 通过整理分出几个重要到模块,其中在这里指出Xc ...

  6. TypeScript在react项目中的实践

    前段时间有写过一个TypeScript在node项目中的实践. 在里边有解释了为什么要使用TS,以及在Node中的一个项目结构是怎样的. 但是那仅仅是一个纯接口项目,碰巧赶上近期的另一个项目重构也由我 ...

  7. Qt Creator项目中使用qss

    近期学习qt .使用的编译器是qt creator ,学习过程中遇到的题就是 怎样将程序中将要用到的.qss 文件静态编译到.exe程序中,而不是在程序执行时动态加载.动态加载的最大问题在于一旦.qs ...

  8. VisualGDB系列11:Linux C++项目中使用外部Linux库

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 在<使用VS创建Linux静态库和 ...

  9. 如何在手机项目中使用rem单位

    rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...

随机推荐

  1. Sharif University CTF 2016 -- Login to System (PWN 200)

    EN: It's easy to find out where is the bug : .text:0000000000400DE4 ; void *start_routine(void *).te ...

  2. delphi SPCOMM串口控件

    在Delphi7.0中安装Spcomm串口通信控件的方法为:选择Delphi7.0的“Component”菜单,点击“Install Component...”菜单项,然后在弹出的Into exist ...

  3. maven的pom.xml配置

    添加tomcat插件配置: <!-- tomcat plugin --> <plugin> <groupId>org.apache.tomcat.maven< ...

  4. Expected one result (or null) to be returned by selectOne(), but found 2

    这个问题在于你查询sql返回结果是多个值.一个集合,但是你在service的实现层的dao都调用了.get方法.而是应该使用.getlist方法等.

  5. R语言画全基因组关联分析中的曼哈顿图(manhattan plot)

    1.在linux中安装好R 2.准备好画曼哈顿图的R脚本即manhattan.r,manhattan.r内容如下: #!/usr/bin/Rscript #example : Rscript plot ...

  6. NGUI 使用EventDelegate.Add与UIInput.onSubmit、UIInput.onChange限定编辑框中的内容

    Unity中,使用NGUI,通常为某个控件(如按钮)绑定事件(单击.双击.拖拽.滚轮.选择等)都是用UIEventListener,比如: public void Bind() { UIEventLi ...

  7. es6之set和map

    1.set ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值,set本身就是构造函数,所以可以通过new的方式来创建对象 // 例一 可以做数组去重用 var set ...

  8. Android内部自带的SQLite数据库操作dos命令

    1:什么叫做SQLite数据库 Android系统内核是Linux系统,Android系统很特殊,他自带了一个SQLite数据库,轻量型的一款嵌入式的数据库 它占用资源非常的低,在嵌入式设备中,可能只 ...

  9. 使用opengl 绘制9个点,理解各个参数的含义

    // SimpleTest1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<gl/glut.h> #incl ...

  10. 对OnOutOfMemoryError的运维处理

    以部署在linux系统/opt/Server目录下的Server.jar为例 1.在run.sh启动脚本中添加jvm参数: -XX:OnOutOfMemoryError=/opt/Server/res ...