论坛收藏整理:C#

*********************************************************************
标题:如何防止一个windows程序被启动多次
来源:
http://community.csdn.net/Expert/topic/2945/2945464.xml?temp=.7424738
*********************************************************************
可以使用互斥体Mutex类型完成此功能。见如下代码:
    [STAThread]
    public static void Main(string[] args)
    {
         //声明互斥体。
         Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
         //判断互斥体是否使用中。
         bool Running = !mutex.WaitOne(0, false);
         if (! Running)
             Application.Run(new FormLogin());
         else
             MessageBox.Show("应用程序已经启动!");
    }

*********************************************************************
标题:操作文本
来源:
http://community.csdn.net/Expert/topic/3122/3122376.xml?temp=1.827419E-03
*********************************************************************
         //读取文本文件
         FileName.Text = filename;
         FileStream fs = new FileStream(Server.MapPath(".")+"
//"+filename, FileMode.Open, FileAccess.Read);
   
         //这里使用默认编码来读取打开文件的内容
         StreamReader sr = new StreamReader(fs,Encoding.Default);
         StringBuilder output = new StringBuilder();
         string rl;
         while((rl=sr.ReadLine())!=null)
         {
              output.Append(rl);
             output.Append("/n");
 
         }
         sr.Close();
         fs.Close();
         filetxt.Text = output;

     //保存文件

   FileStream fw = new FileStream(filePath+fileName, FileMode.Create, FileAccess.Write);
    //建立StreamWrite 为写做准备

    StreamWriter rw = new StreamWriter(fw,Encoding.Default);
    rw.Write(filetxt.Text.ToString());
    rw.Close();
    fw.Close();

用上面代码可以分行读取数据,你更改后可以再保存。

*********************************************************************
标题:如何通过C#编写的程序,来获得一张表的结构
来源:
http://community.csdn.net/Expert/topic/3163/3163386.xml?temp=9.685916E-02
*********************************************************************
   /// <summary>
  /// 返回所有表
  /// </summary>
  /// <returns></returns>
  public static DataTable GetAllTables()
  {
   DataTable dt = new DataTable();

   try
   {
    Con.Open();
    dt = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"Table"});    
   }
   catch(Exception ex)
   {
    throw ex;
   }
   finally
   {
    Con.Close();
   }

   return dt;
  }

  /// <summary>
  /// 返回指定表的结构
  /// </summary>
  /// <param name="varTableName"></param>
  /// <returns></returns>
  public static DataTable GetTheTable(string varTableName)
  {
   DataTable dt = new DataTable();

   DataSet ds = new DataSet();   

   OleDbDataAdapter Odb = new OleDbDataAdapter("select * from " + varTableName,Con);      
   try
   {
    Con.Open();
    Odb.FillSchema(ds,SchemaType.Source,varTableName);
   }
   catch(Exception ex)
   {
    throw ex;
   }
   finally
   {
    Con.Close();
   }
   return ds.Tables[varTableName];    
  }

相关链接:http://dotnet.aspx.cc/ShowDetail.aspx?id=ZP4KXO6X-I697-4I4R-YKYB-6KFVPBC5JJPU

*********************************************************************
标题:
如何判断一个目录为另一个目录的子目录?
来源:http://community.csdn.net/Expert/topic/3161/3161396.xml?temp=.4927637
*********************************************************************
写了个函数
public bool isMDir(DirectoryInfo pDirA,DirectoryInfo pDirB)
  {
   foreach(DirectoryInfo sDir in pDirA.GetDirectories())
   {
    if(sDir==pDirB)
    {
     return true;
    }
   }
   return false;
  }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值