当用作ApplicationDataContainer中的键时,文件路径是否会引发异常的预期行为

时间:2013-03-23 03:24:21

标签: c# windows-8 .net-4.5

我尝试使用文件路径(只是一个字符串)作为ApplicationDataContainer中的键,但它失败了。有3个不同的问题:

  1. 如果密钥是文件路径
  2. ,容器将报告密钥,即使密钥不存在也是如此
  3. 如果您尝试添加一个存在的文件路径的密钥,它就不会执行任何操作。
  4. 如果您尝试添加一个不存在的文件路径的密钥,则会引发异常:
  5. 有没有人见过像这样的问题,这是预期的行为吗?我找不到任何文档路径作为键无效的文档,对于我来说MS似乎很奇怪,因为他们知道并将其视为一个随机字符串,并将其视为文件路径。

     An unhandled exception of type 'System.Exception' occurred in mscorlib.dll
    
     WinRT information: Error trying to query the application data container item info
    
     Additional information: The specified path is invalid.
    

    示例代码:

    var container = ApplicationData.Current.LocalSettings.CreateContainer("SomeContainerName", ApplicationDataCreateDisposition.Always);
    if (container.Values.ContainsKey("C:\\Some_Fake_Path") == false) // This will report as true even though the key is not in the container
          {
            //If you try to run this line it throws an exception that file can not be found even though it is just a key value pair
            container.Values.Add("C:\\Some_Fake_Path", "SomeStuffHere");
    
            //This line won't throw, but it won't actually add a key/value pair either
            container.Values.Add("C:\\This_Path_Actually_Exists\\SomeFile.txt", "SomeStuffHere");
          }
    

0 个答案:

没有答案