Dns获取本机IP地址
public static string GetLocalIP() { try { string HostName = Dns.GetHostName(); //得到主机名 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i = 0; i < IpEntry.AddressList.Length; i++) { //从IP地址列表中筛选出IPv4类型的IP地址 //AddressFamily.InterNetwork表示此IP为IPv4, //AddressFamily.InterNetworkV6表示此地址为IPv6类型 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) { return IpEntry.AddressList[i].ToString(); } } return ""; } catch (Exception ex) { MessageBox.Show("获取本机IP出错:" + ex.Message); return ""; } }
获取当前项目App.config文件下的value值
//获取当前项目App.config文件下的 key=ActiveMQURL 的value值 <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ActiveMQURL" value="tcp://xxxxx:xxxx" /> </appSettings> </configuration> string url = System.Configuration.ConfigurationManager.AppSettings["ActiveMQURL"]; //获取+验证 if (ConfigurationManager.AppSettings["CameraDirver"].ToUpper() == "TOUPCAM") { }
将枚举值赋值到下拉文本框中
//将枚举值赋值到下拉文本框中 //定义静态方法进行转换 public static List<Item> EnumToList<T>() { List<Item> ret = new List<Item>(); foreach (Enum item in Enum.GetValues(typeof(T))) { Item it = new Item(); it.Value = item; it.Name =Enum.GetName(typeof(T),item); ret.Add(it); } return ret; } //枚举声明 public enum SexType { 女=0, 男=1 } //第一种转换方式 tSex.DataSource = CommUtils.Bind.BindUtils.EnumToList<PQCS.Enums.SexType>(); // [Serializable] public class Item { public string Name { get; set; } public Object Value { get; set; } }
读取Json文件
public HttpService HttpService { get; set; } // public LogService() { Dictionary<String, String> keyValuePairs = LocalStorage.DeserializeJSON<Dictionary<String, String>>("LogConfig"); if (true) { keyValuePairs = new Dictionary<string, string>(); keyValuePairs.Add("logServiceUrl", "http://xxxxxx:xxxxx"); //keyValuePairs.Add("logServiceUrl", "http://localhost:8080"); LocalStorage.SerializeJSON(keyValuePairs, "LogConfig"); } this.HttpService = new HttpService(); this.HttpService.HostUrl = keyValuePairs["logServiceUrl"];//HostUrl string属性 }
JSON转换
public static T DeserializeJSON<T>(string name) { byte[] data = ReadFile(name); if (data != null) { string text = Encoding.Default.GetString(data); object obj = Newtonsoft.Json.JsonConvert.DeserializeObject(text, typeof(T)); return (T)obj; } else { return default(T); } } public static void SerializeJSON(Object obj, string name) { string s = Newtonsoft.Json.JsonConvert.SerializeObject(obj); byte[] data = Encoding.Default.GetBytes(s); SaveFile(data, name); }
使用文件流的方式,向文档中添加数据
FileStream 文件流 = new FileStream(蜡块打印路径, FileMode.Create, FileAccess.Write);//搜索创建写入文件 StreamWriter sw = new StreamWriter(文件流, Encoding.Default);//sw(流作者)文件流编码默认 int 蜡块总数 = Convert.ToInt32(totalSample.Text);//起始值(蜡块数量) int 材料数量 = Convert.ToInt32(totalL.Text);//结束值(材料数量) int 取材次数 = 材料数量 - 蜡块总数; string 病理号 = CurrentCaseViewModel.CaseNO; if (取材次数 <= 0) { if (材料数量 > 0) { sw.Write(病理号 + "-" + 材料数量.ToString()); } else { sw.Write(病理号); } sw.Close(); 文件流.Close(); }
将数据写入指定路径文件中
string timestr = DateTime.Now.ToString("yyMMddHHmmssfff"); //写入第一个文件夹 string path = "C:\\" + fileName + "\\" +timestr + "-" + model.CaseNo+"-" + model.Number + ".txt"; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);//搜索创建写入文件 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); sw.Write(model.CaseNo + "," + model.Number + "," + model.Type + "\r\n"); sw.Dispose();
检查文件是否存在,不存在时进行创建
string fileFolder = "C:\\" + fileName; if (!Directory.Exists(fileFolder)) { Directory.CreateDirectory(fileFolder); }
Json转换为对象
StreamReader file = File.OpenText(Environment.CurrentDirectory + "\\UpdateList.json"); Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(file); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); object obj = serializer.Deserialize(reader, typeof(List<UpdateConfig>)); 工作站List = obj as List<UpdateConfig>;