/// <summary> /// 异步加载词典 /// </summary> /// <returns>List<dictlist>(词典类)</returns> public async Task <List <dictlist> > loaddictasync() { List <dictlist> dicts = new List <dictlist>(); FileOpenPicker fop = new FileOpenPicker(); fop.FileTypeFilter.Add(".csv"); StorageFile sf = await fop.PickSingleFileAsync(); dictlist dictlist = new dictlist(); parse_bytes parse_bytes = new parse_bytes(); if (sf != null) { //如果非空,则开始组合词典 if (sf.FileType == ".csv") { IBuffer buffer = await FileIO.ReadBufferAsync(sf); using (DataReader dataReader = DataReader.FromBuffer(buffer)) { byte[] csvsrc = new byte[dataReader.UnconsumedBufferLength]; dataReader.ReadBytes(csvsrc); string encoding = parse_bytes.DetectUnicode(csvsrc); string csv = parse_bytes.byte2str(csvsrc, encoding); dicts = dictlist.parse_csvdict(csv); } } return(dicts); } else { //否则返回空值 return(null); } }
/// <summary> /// 打开 .ks 文件 /// </summary> /// <returns>string files.filename(文件名)、byte[] files.srcode(文件的二进制内容)</returns> public async Task <files> load_ksasync() { // 打开文件 FileOpenPicker fop = new FileOpenPicker(); fop.FileTypeFilter.Add(".ks"); fop.FileTypeFilter.Add(".txt"); StorageFile sf = await fop.PickSingleFileAsync(); byte[] src; files files; string filename; if (sf != null) { //把文件转换为 ibuffer IBuffer buffer = await FileIO.ReadBufferAsync(sf); parse_bytes parse_bytes = new parse_bytes(); //以二进制方式读取文件 using (DataReader datareader = DataReader.FromBuffer(buffer)) { //DataReader datareader = DataReader.FromBuffer(buffer); src = new byte[datareader.UnconsumedBufferLength]; datareader.ReadBytes(src); } filename = sf.Name; } else { //由于直接返回 null 会导致 System.NullReferenceException,所以随便返回一个 src = Encoding.ASCII.GetBytes("error"); filename = "empty"; } files.filename = filename; files.srcode = src; return(files); }