热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

winfromC#树勾选等

AfterCheck树勾选privatevoidt

AfterCheck

///

/// 树勾选/// /// /// private void tvCourse_AfterCheck(object sender, TreeViewEventArgs e){if (e.Action ==TreeViewAction.ByMouse){if (e.Node.Checked){SetChildNode(e.Node, true);if (null!= e.Node.Parent){SetParentNode(e.Node, true);}//获取选中节点值
lstChapterIds.Add(e.Node.Name);this.btnDelRes.Enabled = true;}else{//取消节点选中状态之后,取消所有父节点的选中状态SetChildNode(e.Node, false);//如果节点存在父节点,取消父节点的选中状态if (e.Node.Parent != null){SetParentNode(e.Node, false);}//获取选中节点值if (lstChapterIds.Contains(e.Node.Name)){lstChapterIds.Remove(e.Node.Name);if (!lstChapterIds.Any()){this.btnDelRes.Enabled = false;}}}}}
AfterCheck

设置节点

///

/// 查找根节点/// /// 节点node/// private TreeNode FindParentNode(TreeNode node){if (node.Parent == null){return node;}else{return FindParentNode(node);}}/// /// 设置父节点/// /// 当前节点/// 状态private void SetParentNode(TreeNode node, bool state){TreeNode pNode = node.Parent;if (null != pNode){pNode.Checked = state;bool flag = false;foreach (TreeNode currNode in pNode.Nodes){if (!currNode.Checked) //控制父节点全选,父节点选中去掉!
{flag = false;pNode.Checked = flag;break;}//SetParentNode(item, state);
}if (pNode.Checked ){//获取选中节点值
lstChapterIds.Add(pNode.Name);}else{if (lstChapterIds.Contains(pNode.Name)){lstChapterIds.Remove(pNode.Name);}}SetParentNode(pNode, flag);}}/// /// 设置子节点/// /// 当前节点/// 状态private void SetChildNode(TreeNode node, bool state){TreeNodeCollection tncNodes = node.Nodes;if (tncNodes.Count>0){foreach (TreeNode currNode in tncNodes){currNode.Checked = state;if (currNode.Checked){//获取选中节点值
lstChapterIds.Add(currNode.Name);}else{if (lstChapterIds.Contains(currNode.Name)){lstChapterIds.Remove(currNode.Name);}}SetChildNode(currNode, state);}}}
View Code

 

程序只开一个:program.cs

 

bool createdNew = false;System.Threading.Mutex instance = new System.Threading.Mutex(true, Application.ProductName, out createdNew);if (createdNew){//...........code
instance.ReleaseMutex();}else{MessageBox.Show("该程序已经在运行!", Application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Exclamation);Application.Exit();}

View Code

 

委托 不同线程间访问控件

private delegate void UploadPro(int mProgre);private event UploadPro UploadProEvent;UploadProEvent +=ResourceManage_UploadProEvent;void ResourceManage_UploadProEvent(int mProgree)
{
//......

}
var thread = new Thread(new ThreadStart(method));
thread.Start();

View Code

this.Invoke(UploadProEvent,currentPro);

 

解压缩 文件

ZipInputStream inputStream = new ZipInputStream(File.OpenRead(strFilePath));
ZipEntry theEntry;
//读取文件名List<string> fileNameList &#61; new List<string>();while ((theEntry &#61; inputStream.GetNextEntry()) !&#61; null){totalSize &#43;&#61; theEntry.UncompressedSize;string templateName &#61; theEntry.FileName;string fileName &#61; Path.GetFileName(templateName);if (!string.IsNullOrEmpty(fileName))fileNameList.Add(fileName);}inputStream.Close();#region 解压文件inputStream &#61; new ZipInputStream(File.OpenRead(filePath));//long totalSize &#61; inputStream.Length;long currentSize &#61; 0;int currentPro &#61; 0;ZipEntry theEntry;ZipFile zf &#61; ZipFile.Read(filePath);//以文件流来解压所有文件&#xff0c;同时计算进度while ((theEntry &#61; inputStream.GetNextEntry()) !&#61; null){//totalSize &#43;&#61; theEntry.UncompressedSize;//long totalSize &#61; theEntry.UncompressedSize;//destPath &#43;&#61; "/";//string templateName &#61; theEntry.FileName;//string fileName &#61; Path.GetFileName(templateName);////string dir &#61; Path.GetDirectoryName(templateName);//string dir &#61; templateName;//if (!dir.Contains("\\") && string.IsNullOrEmpty(fileName))//{// continue;//} ////截取目录文件夹//if (dir.Contains("\\"))// dir &#61; dir.Substring(dir.IndexOf("\\") &#43; 1, dir.Length - dir.IndexOf("\\") - 1);//if (templateName.Length &#61;&#61; (dir &#43; "/" &#43; fileName).Length) dir &#61; "";////theEntry.IsDirectory 是否目录//string path &#61; Path.GetDirectoryName(destPath) &#43; "/" &#43; dir &#43; "/";////Path.GetDirectoryName(baseDir) &#43; "/";////if (!Directory.Exists(path)) // Directory.CreateDirectory(path);string strFilePath &#61; string.Empty;string templateName &#61; theEntry.FileName;if (!templateName.Contains("\\")){//文件strFilePath &#61; destPath &#43; "\\" &#43; theEntry.FileName;}else{//文件夹里文件strFilePath &#61; destPath &#43; theEntry.FileName;}if (!Directory.Exists(Path.GetDirectoryName(strFilePath))){Directory.CreateDirectory(Path.GetDirectoryName(strFilePath));}string fileName &#61; Path.GetFileName(templateName);if (fileName !&#61; String.Empty){FileStream streamWriter &#61; File.OpenWrite(strFilePath); //解压文件到指定的目录 int size &#61; 0;byte[] data &#61; new byte[2048];try{while (true){size &#61; inputStream.Read(data, 0, data.Length);if (size > 0){streamWriter.Write(data, 0, size);currentSize &#43;&#61; size;}else{break;}currentPro &#61; Convert.ToInt32(100 * currentSize / totalSize);this.Invoke(UploadProEvent,currentPro);}}catch (Exception ex){LogHelper.Error(this.GetType(), ex);this.Invoke(UploadProEvent, -1); //-1 导入失败
}finally{streamWriter.Close();}}}inputStream.Close();#endregion

View Code

 

检查指导文件盘符&#xff0c;获取空间大盘符

 

///

/// 检查指导盘符剩余空间/// /// /// public static long GetHardDiskFreeSpace(string str_HardDiskName){if (string.IsNullOrEmpty(str_HardDiskName)) return 0;long freeSpace &#61; new long();System.IO.DriveInfo[] drives &#61; System.IO.DriveInfo.GetDrives();foreach (System.IO.DriveInfo drive in drives){if (drive.Name &#61;&#61; str_HardDiskName){freeSpace &#61; drive.TotalFreeSpace;}}return freeSpace;}/// /// 找出当前客户端剩余空间最大盘符/// /// public static Dictionary<string, long> GetSystemDirtory(){Dictionary<string, long> freeSpace &#61; new Dictionary<string, long>();DriveInfo[] drives &#61; DriveInfo.GetDrives();foreach (DriveInfo drive in drives){if (drive.DriveType &#61;&#61; DriveType.Fixed)freeSpace.Add(drive.Name, drive.TotalFreeSpace);}string hardDiskName &#61; freeSpace.ToArray().Max(x &#61;> x.Key);long hardDiskSize &#61; freeSpace.ToArray().Max(x &#61;> x.Value);freeSpace.Clear();freeSpace.Add(hardDiskName, hardDiskSize);return freeSpace;}
View Code

 

获取MAC地址

///

/// 获取网卡MAC地址/// /// public static string GetNetCardMAC(){try{string stringMAC &#61; "";ManagementClass MC &#61; new ManagementClass("Win32_NetworkAdapterConfiguration");ManagementObjectCollection MOC &#61; MC.GetInstances();foreach (ManagementObject MO in MOC){if ((bool)MO["IPEnabled"] &#61;&#61; true){stringMAC &#43;&#61; MO["MACAddress"].ToString();}}return stringMAC;}catch{return "";}}
View Code

 

获取远程文件信息

///

/// 获取远程文件信息/// /// /// public static RemoteFileInfo GetFileInfo(string url){RemoteFileInfo result &#61; new RemoteFileInfo();try{GC.Collect();HttpWebRequest request &#61; (HttpWebRequest)WebRequest.Create(url);HttpWebResponse response &#61; (HttpWebResponse)request.GetResponse();result.MimeType &#61; response.ContentType;result.LastModified &#61; response.LastModified;result.FileSize &#61; response.ContentLength;result.AcceptRanges &#61; String.Compare(response.Headers["Accept-Ranges"], "bytes", true) &#61;&#61; 0;//关闭连接
response.Close();request.KeepAlive &#61; false;}catch (Exception ex){throw;}return result;}
View Code

 

错误日志

#region 日志方法public void LogTest(string str){string path &#61; System.IO.Directory.GetCurrentDirectory() &#43; "\\log\\";if (!Directory.Exists(path)){DirectoryInfo directorylnfo &#61; new DirectoryInfo(path);directorylnfo.Create();}string dt &#61; "\r\n时间&#xff1a;" &#43; DateTime.Now.ToString("yyyymmddhhmmss") &#43; "\r\n内容&#xff1a;" &#43; str;int len &#61; dt.Length;byte[] inputByts &#61; new byte[len];inputByts &#61; Encoding.GetEncoding("utf-8").GetBytes(dt);System.IO.FileStream fs &#61; new System.IO.FileStream(path &#43; "log.txt", System.IO.FileMode.OpenOrCreate);fs.Seek(0, System.IO.SeekOrigin.End);fs.Write(inputByts, 0, inputByts.Length);fs.Close();//关闭流
} #endregion

View Code

 

///

/// 错误日志/// /// public static void WriteLog(Exception ex){string logPath &#61; GetResDir() &#43; "/log";string message &#61; string.Format("\r\n\r\n异常时间&#xff1a;{0} \r\n错误源&#xff1a;{1} \r\n异常信息&#xff1a;{2}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff"), ex.Source, ex.Message);File.AppendAllText(logPath &#43; DateTime.Now.ToString("yyyy.MM.dd") &#43; ".log", message, Encoding.UTF8);}
View Code

 

启动停止服务

 

///

/// 启动服务sc.DisplayName/// /// public static void StartService(string serverName){if (string.IsNullOrEmpty(serverName)) return;ServiceController sc &#61; new ServiceController(serverName);if (sc.DisplayName !&#61; null){if (sc.Status &#61;&#61; ServiceControllerStatus.Stopped) sc.Start();}}/// /// 停止服务/// /// public static void StopService(string serverName){if (string.IsNullOrEmpty(serverName)) return;ServiceController sc &#61; new ServiceController(serverName);if (sc.DisplayName !&#61; null) {if (sc.Status &#61;&#61; ServiceControllerStatus.Running) sc.Stop();}}
View Code

 

插入修改表数据

public string InsertORupdateData(List _list, string dtName,string columnName){StringBuilder sbInsert &#61; new StringBuilder();ArrayList list &#61; new ArrayList();try{string sqlstr &#61; "";if (_list !&#61; null && _list.Count > 0){System.Reflection.PropertyInfo[] p &#61; _list[0].GetType().GetProperties();int length &#61; 0;string values &#61; "";for (int i &#61; 0; i <_list.Count; i&#43;&#43;){string sbUpdateSql &#61; "";int count &#61; 0;string upwh &#61; "";length &#61; 0;//通过反射获取list中的字段 foreach (System.Reflection.PropertyInfo pi in p){string cName &#61; pi.Name;string cValue &#61; (pi.GetValue(_list[i], null) &#61;&#61; null? "": (pi.GetValue(_list[i], null)).ToString().Replace("&#39;", "")); //替换 &#39;语法错误的if (cName.ToLower() &#61;&#61; columnName.ToLower()){string sql &#61; "select count(0) from " &#43; dtName &#43; " where " &#43; columnName &#43; "&#61;&#39;"&#43;cValue&#43;"&#39;";upwh &#61; " where " &#43; columnName &#43; "&#61;&#39;" &#43; cValue &#43; "&#39;";count &#61; ExcelHelper.GetScalar(sql);}if (length &#61;&#61; 0){sqlstr &#61; "INSERT INTO " &#43; dtName &#43; " (";values &#61; "(";sbUpdateSql&#43;&#61;"update " &#43; dtName &#43; " set ";}length &#43;&#61; 1;if (length < p.Length){sbUpdateSql &#43;&#61; cName &#43; "&#61; &#39;" &#43; cValue &#43;"&#39;,";sqlstr &#43;&#61; cName &#43; ",";values &#43;&#61; "&#39;" &#43; cValue &#43; "&#39;,";}else{sbUpdateSql &#43;&#61; cName &#43; "&#61; &#39;" &#43; cValue &#43; "&#39;";sqlstr &#43;&#61; cName &#43; ") Values";values &#43;&#61; "&#39;" &#43; cValue &#43; "&#39;); ";if (count > 0){sqlstr &#61; sbUpdateSql &#43; upwh;values &#61; "";}}}sqlstr &#43;&#61; values;list.Add(sqlstr);}}//for (int i &#61; 0; i //{// ArrayList arrList &#61; new ArrayList();// arrList.Add(list[i]);// var result &#61; AccessDbHelper.ExecuteSqlTran(arrList);//}//string str &#61; string.Join("", list.ToArray());var result &#61; AccessDbHelper.ExecuteSqlTran(list);return "success";}catch (Exception err){LogTest(err.ToString());return err.ToString();}}

View Code

 

转:https://www.cnblogs.com/love201314/p/6307593.html



推荐阅读
  • C#实现文件的压缩与解压
    2019独角兽企业重金招聘Python工程师标准一、准备工作1、下载ICSharpCode.SharpZipLib.dll文件2、项目中引用这个dll二、文件压缩与解压共用类 ... [详细]
  • 机器学习算法:SVM(支持向量机)
    SVM算法(SupportVectorMachine,支持向量机)的核心思想有2点:1、如果数据线性可分,那么基于最大间隔的方式来确定超平面,以确保全局最优, ... [详细]
  • 本文介绍了如何将包含复杂对象的字典保存到文件,并从文件中读取这些字典。 ... [详细]
  • 目录预备知识导包构建数据集神经网络结构训练测试精度可视化计算模型精度损失可视化输出网络结构信息训练神经网络定义参数载入数据载入神经网络结构、损失及优化训练及测试损失、精度可视化qu ... [详细]
  • 2020年9月15日,Oracle正式发布了最新的JDK 15版本。本次更新带来了许多新特性,包括隐藏类、EdDSA签名算法、模式匹配、记录类、封闭类和文本块等。 ... [详细]
  • 本文总结了Java初学者需要掌握的六大核心知识点,帮助你更好地理解和应用Java编程。无论你是刚刚入门还是希望巩固基础,这些知识点都是必不可少的。 ... [详细]
  • Android 自定义 RecycleView 左滑上下分层示例代码
    为了满足项目需求,需要在多个场景中实现左滑删除功能,并且后续可能在列表项中增加其他功能。虽然网络上有很多左滑删除的示例,但大多数封装不够完善。因此,我们尝试自己封装一个更加灵活和通用的解决方案。 ... [详细]
  • 本文详细介绍了Java反射机制的基本概念、获取Class对象的方法、反射的主要功能及其在实际开发中的应用。通过具体示例,帮助读者更好地理解和使用Java反射。 ... [详细]
  • 零拷贝技术是提高I/O性能的重要手段,常用于Java NIO、Netty、Kafka等框架中。本文将详细解析零拷贝技术的原理及其应用。 ... [详细]
  • 本文介绍如何使用 Python 的 DOM 和 SAX 方法解析 XML 文件,并通过示例展示了如何动态创建数据库表和处理大量数据的实时插入。 ... [详细]
  • 重要知识点有:函数参数默许值、盈余参数、扩大运算符、new.target属性、块级函数、箭头函数以及尾挪用优化《深切明白ES6》笔记目次函数的默许参数在ES5中,我们给函数传参数, ... [详细]
  • 深入解析 Lifecycle 的实现原理
    本文将详细介绍 Android Jetpack 中 Lifecycle 组件的实现原理,帮助开发者更好地理解和使用 Lifecycle,避免常见的内存泄漏问题。 ... [详细]
  • 开发日志:高效图片压缩与上传技术解析 ... [详细]
  • 在《Cocos2d-x学习笔记:基础概念解析与内存管理机制深入探讨》中,详细介绍了Cocos2d-x的基础概念,并深入分析了其内存管理机制。特别是针对Boost库引入的智能指针管理方法进行了详细的讲解,例如在处理鱼的运动过程中,可以通过编写自定义函数来动态计算角度变化,利用CallFunc回调机制实现高效的游戏逻辑控制。此外,文章还探讨了如何通过智能指针优化资源管理和避免内存泄漏,为开发者提供了实用的编程技巧和最佳实践。 ... [详细]
  • 如何将TS文件转换为M3U8直播流:HLS与M3U8格式详解
    在视频传输领域,MP4虽然常见,但在直播场景中直接使用MP4格式存在诸多问题。例如,MP4文件的头部信息(如ftyp、moov)较大,导致初始加载时间较长,影响用户体验。相比之下,HLS(HTTP Live Streaming)协议及其M3U8格式更具优势。HLS通过将视频切分成多个小片段,并生成一个M3U8播放列表文件,实现低延迟和高稳定性。本文详细介绍了如何将TS文件转换为M3U8直播流,包括技术原理和具体操作步骤,帮助读者更好地理解和应用这一技术。 ... [详细]
author-avatar
单身男人adgjm
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有