【发布时间】:2018-01-10 10:19:44
【问题描述】:
我正在尝试在 Windows 10 cordova 应用程序中使用两个不同的 Windows UWP 项目。两个项目都产生相同的错误。其中一个项目的代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
namespace DiskSpaceLibrary
{
public sealed class DiskSpace
{
internal static readonly StorageFolder[] APP_FOLDERS = {
ApplicationData.Current.LocalFolder,
ApplicationData.Current.RoamingFolder,
ApplicationData.Current.TemporaryFolder
};
[DataContract]
internal class Result
{
[DataMember]
internal ulong app = 0;
[DataMember]
internal ulong total = 0;
[DataMember]
internal ulong free = 0;
}
// Manually compute total size of the given StorageFolder
private static ulong sizeFolder(StorageFolder folder)
{
ulong folderSize = 0;
try
{
DirectoryInfo dirInfo = new DirectoryInfo(folder.Path);
// Get back a prefilled (with size) list of files contained in given folder
foreach (var fileInfo in dirInfo.EnumerateFiles("*", SearchOption.AllDirectories))
{
folderSize += (ulong)fileInfo.Length;
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
return 0;
}
return folderSize;
}
// Return the system FreeSpace and Capacity properties
private async static Task<IDictionary<string, object>> getExtraProperties()
{
var basicProperties = await Windows.Storage.ApplicationData.Current.LocalFolder.GetBasicPropertiesAsync();
return await basicProperties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });
}
// Class Entry point
public static IAsyncOperation<string> info(string args)
{
// Entry point in WinRT can not return Task<T> - so here is a trick to convert IAsyncOperation (WinRT) into classic C# async
return infoTask().AsAsyncOperation();
}
// The real disk space work is done here within some asynchronous stuff
private async static Task<string> infoTask()
{
Result result = new Result();
// Run folder discovery into another Thread to not block UI Thread
await Task.Run(() => {
foreach (var folder in APP_FOLDERS)
{
result.app += sizeFolder(folder);
}
});
await getExtraProperties().ContinueWith(propertiesTask =>
{
result.free = (ulong)propertiesTask.Result["System.FreeSpace"];
result.total = (ulong)propertiesTask.Result["System.Capacity"];
});
// Return JSON Result
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Result));
MemoryStream outputMs = new MemoryStream();
serializer.WriteObject(outputMs, result);
outputMs.Position = 0;
StreamReader sr = new StreamReader(outputMs);
return sr.ReadToEnd();
}
}
}
此项目来自https://github.com/sqli/sqli-cordova-disk-space-plugin
它只是查找已用磁盘空间,并使用 System.IO.DirectoryInfo 和异步方法报告已用和可用空间,以获取可用空间和容量。
当我构建时,它生成 winmd 文件没有问题,我已在我的 cordova (Visual Studio 2017) 项目中正确注册并加载这些文件,构建 Windows 10 并运行 - 当我在主 cordova 项目中运行此代码时
DiskSpacePlugin.info({}, function (ok) { console.log(ok); }, function (err) { console.log(err); });
我在控制台中得到这个结果
WinRTError: The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at DiskSpaceLibrary.DiskSpace.sizeFolder(StorageFolder folder)
at DiskSpaceLibrary.DiskSpace.<>c__DisplayClass5_0.<infoTask>b__0()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.Ex
console-via-logger.js (173,15)
{
[functions]: ,
__proto__: { },
asyncOpCausalityId: 461,
asyncOpSource: { },
asyncOpType: "Windows.Foundation.IAsyncOperation`1<String>",
description: "The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at DiskSpaceLibrary.DiskSpace.sizeFolder(StorageFolder folder)
at DiskSpaceLibrary.DiskSpace.<>c__DisplayClass5_0.<infoTask>b__0()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.Ex",
message: "The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at DiskSpaceLibrary.DiskSpace.sizeFolder(StorageFolder folder)
at DiskSpaceLibrary.DiskSpace.<>c__DisplayClass5_0.<infoTask>b__0()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.Ex",
name: "WinRTError",
number: -2147024894
}
我还尝试了另一个模块来解压缩cordova windows 10项目中的文件(我没有这个的源,但是它来自https://github.com/Culture22/cordova-windows10-zip),当我尝试时它会出现完全相同的错误消息在应用程序中运行它。
在过去的 4 天里,我在谷歌上搜索并寻找了这个问题,但我无法取得进展,所使用的方法都得到了框架的支持,显然正在查看 (msdn.microsoft.com/en-us/library/windows /apps/mt185496.aspx) - 有没有人有任何见解?
【问题讨论】:
-
System.IO.FileSystem 不是框架的一部分,它是一个 nuget 包nuget.org/packages/System.IO.FileSystem
-
是的,我明白这一点,甚至已经将其加载到项目中,但根据 MS 文档,它甚至不应该使用它?
标签: c# cordova uwp win-universal-app cordova-plugins