forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPclExport.WinStore.cs
More file actions
131 lines (108 loc) · 3.96 KB
/
PclExport.WinStore.cs
File metadata and controls
131 lines (108 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Copyright (c) Service Stack LLC. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
#if NETFX_CORE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace ServiceStack
{
public class WinStorePclExport : PclExport
{
public new static WinStorePclExport Provider = new WinStorePclExport();
public WinStorePclExport()
{
this.PlatformName = Platforms.WindowsStore;
}
public static PclExport Configure()
{
Configure(Provider);
return Provider;
}
public override string ReadAllText(string filePath)
{
var task = Windows.Storage.StorageFile.GetFileFromPathAsync(filePath);
task.AsTask().Wait();
var file = task.GetResults();
var streamTask = file.OpenStreamForReadAsync();
streamTask.Wait();
var fileStream = streamTask.Result;
return new StreamReader(fileStream).ReadToEnd();
}
public override bool FileExists(string filePath)
{
try
{
var task = Windows.Storage.StorageFile.GetFileFromPathAsync(filePath);
//no exception means file exists
return true;
}
catch (Exception ex)
{
//find out through exception
return false;
}
}
public override void WriteLine(string line)
{
System.Diagnostics.Debug.WriteLine(line);
}
public override void WriteLine(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(format, args);
}
public override Assembly[] GetAllAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
private sealed class AppDomain
{
public static AppDomain CurrentDomain { get; private set; }
public static Assembly[] cacheObj = null;
static AppDomain()
{
CurrentDomain = new AppDomain();
}
public Assembly[] GetAssemblies()
{
return cacheObj ?? GetAssemblyListAsync().Result.ToArray();
}
private async System.Threading.Tasks.Task<IEnumerable<Assembly>> GetAssemblyListAsync()
{
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assemblies = new List<Assembly>();
foreach (Windows.Storage.StorageFile file in await folder.GetFilesAsync())
{
if (file.FileType == ".dll" || file.FileType == ".exe")
{
try
{
var filename = file.Name.Substring(0, file.Name.Length - file.FileType.Length);
AssemblyName name = new AssemblyName() { Name = filename };
Assembly asm = Assembly.Load(name);
assemblies.Add(asm);
}
catch (Exception)
{
// Invalid WinRT assembly!
}
}
}
cacheObj = assemblies.ToArray();
return cacheObj;
}
}
public override string GetAssemblyCodeBase(Assembly assembly)
{
return assembly.GetName().FullName;
}
//public override DateTime ToStableUniversalTime(DateTime dateTime)
//{
// // .Net 2.0 - 3.5 has an issue with DateTime.ToUniversalTime, but works ok with TimeZoneInfo.ConvertTimeToUtc.
// // .Net 4.0+ does this under the hood anyway.
// return TimeZoneInfo.ConvertTimeToUtc(dateTime);
//}
}
}
#endif