标签/分类 "CSharp" 下的文章
CSharp 一些代码小结--文件对话框
C# 一些代码小结–文件对话框 查看文件完整路径 try { Config cfg = new Config(); var file = ""; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { file = saveFileDialog1.FileName;//(获取整个文件名) cfg.WriteConfigFile(command, file); MessageBox.Show("file:" + file); } } catch (Exception) { OutputMessage("检查软件路径下面是否
CSharp 一些代码小结--UI操作
C# 一些代码小结–UI操作 使用控件名调用控件 object obj = this.GetType().GetField("控件名", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); ComboBox cb = (ComboBox)obj; 在控件触发事件时判断触发事件的控件 TextBox text = sender as TextBox; int id = 0; String str = null; switch (text.Name) 跨线程使用委托调用U
CSharp 一些代码小结--使用文件记录日志
C# 一些代码小结–使用文件记录日志 public class FaceLog { public static void AppendInfoLog(string errMsg) { try { string Folder = ".\\log\\"; string fileName = Folder + "Info_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (!System.IO.Directory.Exists(Folder)) System.IO.Directory.CreateDirectory(Folder); if (!File.Exists(fileName)) { FileStream stream = System.IO.File.Create(fileName); stream.Close(); stream.Dispose(); } using (TextWriter fs = new StreamWriter(fileName, true)) { fs.Write(errMsg); fs.Close(); fs.Dispose(); } } catch { } } public static void AppendSerialLog(string errMsg) { try { string Folder = ".\\log\\"; string fileName = Folder + "Serial_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (!System.IO.Directory.Exists(Folder)) System.IO.Directory.CreateDirectory(Folder); if (!File.Exists(fileName)) System.IO.File.Create(fileName); using (TextWriter fs = new StreamWriter(fileName, true)) { fs.Write(errMsg); fs.Close(); fs.Dispose(); } } catch
CSharp 一些代码小结--串口操作
串口解析显示中文 private String SerialPortReadStr() { try { String str = null; int n = serialPort1.BytesToRead;//数据接收缓存区的字接数 byte[] buffer = new byte[n];//创建临时字节数组 serialPort1.Read(buffer, 0, n);//从缓存区读取数据 str = Encoding.GetEncoding("GB2312").GetString(buffer); if(!IsStopReadComData) { UpDateLogTextBox(str); } if (保存串口日志ToolStripM
CSharp 一些代码小结--datGirdView保存到csv文件
if (dataGridView1.Rows.Count == 0) { MessageBox.Show("No data available!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.FileName = null; saveFileDialog.Title = "Save path of the file to be exported"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { Stream myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string strLine = ""; try { //Write in the headers of the columns. for (int i = 0; i < dataGridView1.ColumnCount; i++) { if (i > 0) strLine += ","; strLine += dataGridView1.Columns[i].HeaderText; } strLine.Remove(strLine.Length - 1); sw.WriteLine(strLine); strLine = ""; //Write in the content of the columns. for (int j = 0; j < dataGridView1.Rows.Count; j++) {
CSharp读取json配置文件内容
步骤 读取配置文件转换成字符串,代码如下 string contents = System.IO.File.ReadAllText("config.json"); 注意:该语句会抛出文件不存在异常。 使用Newtonsoft.Json将json字符串转换成类的对象,完整代码如下所示 public class Init { public static InitInfo init = new InitInfo(); public Init(){ try { string contents = System.IO.File.ReadAllText("config.json"); //Shell.WriteLine("ini 配置文件\r\n" + JsonConvert.SerializeObject(init)); init = JsonConvert.DeserializeObject<InitInfo>(contents); S