Created
January 26, 2017 19:44
-
-
Save juanpaexpedite/6433a914f523aae5a278d5d909600796 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Windows.Storage; | |
using Windows.UI.Xaml; | |
namespace Universal.Services | |
{ | |
public class ResourceFilesManager | |
{ | |
#region Build Action: Embedded | |
public static Stream LoadEmbeddedStreamAsync(Type type, string contenturi) | |
{ | |
var assembly = type.GetTypeInfo().Assembly; | |
var relativepath = ConvertContentEmbedded(contenturi); | |
var embeddedpath = $"{type.Namespace}.{relativepath}"; | |
var stream = assembly.GetManifestResourceStream(embeddedpath); | |
return stream; | |
} | |
public static async Task<string> LoadEmbeddedTextFileAsync(Type type, string contenturi) | |
{ | |
var stream = LoadEmbeddedStreamAsync(type, contenturi); | |
return await ReadStringFromStreamAsync(stream); | |
} | |
#endregion | |
#region Helpers | |
private static string ConvertContentEmbedded(string contenturi) | |
{ | |
var prefix = "ms-appx:"; | |
var relativeuri = contenturi.Replace(prefix, String.Empty).TrimStart('/'); | |
return relativeuri.Replace('/', '.'); | |
} | |
private static (string AppNameSpace, Assembly AppAssembly) EnvironmentFromApp(Type type) | |
{ | |
var appSpace = type.Namespace; | |
var assembly = type.GetTypeInfo().Assembly; | |
return (appSpace, assembly); | |
} | |
private static async Task<string> ReadStringFromStreamAsync(Stream stream) | |
{ | |
String content = null; | |
using (var reader = new StreamReader(stream)) | |
{ | |
content = await reader.ReadToEndAsync(); | |
} | |
return content; | |
} | |
#endregion | |
#region Build Action: Content | |
public static async Task<string> LoadContentTextFileAsync(string contenturi) | |
{ | |
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(contenturi)); | |
return await FileIO.ReadTextAsync(file); | |
} | |
public static async Task<bool> DeleteContentFile(string contenturi) | |
{ | |
try | |
{ | |
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(contenturi)); | |
if (file != null) | |
{ | |
await file.DeleteAsync(StorageDeleteOption.PermanentDelete); | |
return true; | |
} | |
return false; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
#endregion | |
#region Local File | |
public static async Task CopyResourceToLocal(Type type, string resourcepath, string filename) | |
{ | |
var localFolder = ApplicationData.Current.LocalFolder; | |
var dbFile = await localFolder.TryGetItemAsync(filename) as StorageFile; | |
if (null == dbFile) | |
{ | |
var stream = LoadEmbeddedStreamAsync(type, resourcepath); | |
//Copy the stream | |
var storageFile = await localFolder.CreateFileAsync(filename); | |
using (var outputStream = await storageFile.OpenStreamForWriteAsync()) | |
{ | |
await stream.CopyToAsync(outputStream); | |
} | |
} | |
} | |
public static async Task<bool> DeleteLocalFile(string filename) | |
{ | |
try | |
{ | |
var localFolder = ApplicationData.Current.LocalFolder; | |
var file = await localFolder.TryGetItemAsync(filename); | |
if (file != null) | |
{ | |
await file.DeleteAsync(StorageDeleteOption.PermanentDelete); | |
return true; | |
} | |
return false; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment