文章目录 Dns获取本机IP地址 获取当前项目App.config文件下的value值 将枚举值赋值到下拉文本框中 读取Json文件 JSON转换 使用文件流的方式,向文档中添加数据 将数据写入指定路径文件中 检查文件是否存在,不存在时进行创建 Json转换为对象
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++ ) { 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值
< ? 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" ) ; LocalStorage. SerializeJSON ( keyValuePairs, "LogConfig" ) ; } this . HttpService = new HttpService ( ) ; this . HttpService. HostUrl = keyValuePairs[ "logServiceUrl" ] ;
}
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) ;
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> ;