【问题标题】:Windows Universal App Error System.IO.FileSystem - CordovaWindows 通用应用程序错误 System.IO.FileSystem - Cordova
【发布时间】: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


【解决方案1】:

您引用的库似乎引用了 System.IO.FileSystem.dll,但无论出于何种原因,它都没有与您的应用程序一起部署。我建议尝试向您的应用程序添加对 System.IO.FileSystem.dll 的 nuget 引用。

【讨论】:

  • 我相信 Cordova 7 会以一种称为远程模式的方式创建 Windows 10 UWP 应用程序——它似乎不支持包含其他库。仍在处理整个事情,但在我删除了外部库并使用 UWP 较差的库而不是像 Windows.Data.Json 进行序列化并使用 DIrectoryInfo 而不是使用 Windows 在文件夹中取出循环之后,能够编译和运行上述内容。 Storage.FileProperties.BasicProperties basicProperties = await file.GetBasicPropertiesAsync(); - 我不知道如何解决这个问题。
  • 我刚刚在另一个问题上找到了这条评论:Cordova UWP 应用程序只能使用 NetCore,而不是完整的 .Net 框架,无论它们是直接的 .Net 应用程序还是在 WRC 中调用 .Net
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 2016-08-13
  • 2018-08-12
  • 2011-12-29
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
相关资源
最近更新 更多