UWP 读取临时文件和文档
UWP read temporary files and documentation
我尝试使用 windows 应用程序文档读取 UWP 上的临时文件(读写)。但是我无法使用下面 link
示例中的文档使其工作
Documentation
在这部分:
async void WriteTimestamp()
{
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter =
new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt",
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTimeOffset.Now));
}
但很快我就遇到了“temporaryFolder”的问题。据说在当前上下文中不存在。所以...
我尝试:(1) 添加“使用 Windows.Storage;”不工作...
(2) 对外声明:
public Windows.Storage.StorageFolder temporaryFolder { get; set; }
这次代码说没有错误,但是当我启动程序时它停止了。 " 'Object reference not set to an instance of an object.'
App12UIWindows.Page2.temporaryFolder.get return 空。
所以我从这里 (Whosebug) 尝试了一个解决方案并且它有效。 Link:
Alternative Solution
我的问题是:为什么会出错?文档是正确的还是其他?
还是我在错误的地方搜索?我的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using Windows.Storage;
using System.Threading.Tasks;
namespace App12UIWindows
{
/// <summary>
/// </summary>
public sealed partial class Page2 : Page
{
public Page2()
{
this.InitializeComponent();
SaveTempFunc();
readMyFileTemp();
}
public Windows.Storage.StorageFolder temporaryFolder { get; set; }
async void SaveTempFunc()
{
StorageFile salvandoClientesT = await
temporaryFolder.CreateFileAsync("textOf_Information.txt") ;
await FileIO.WriteTextAsync(salvandoClientesT,"test .. just like that... and
anothers words to a file");
}
//Fim salvando memória temporária
//Lendo na memória temporária arquivo de texto
async void readMyFileTemp()
{
try
{
StorageFile lendoTextoSalvo = await
temporaryFolder.GetFileAsync("textOf_Information.txt");
String timestamp = await FileIO.ReadTextAsync(lendoTextoSalvo);
}
catch (Exception)
{
}
}
}
}
抱歉英语不好:)
'Object reference not set to an instance of an object.' App12UIWindows.Page2.temporaryFolder.get return null."
问题是您只是声明了 temporaryFolder
,但没有应用对 temporaryFolder
的引用。请参考上面提到的document。将 ApplicationData.Current.TemporaryFolder
传递给临时文件夹变量。
public Windows.Storage.StorageFolder temporaryFolder { get; set; } = ApplicationData.Current.TemporaryFolder;
我尝试使用 windows 应用程序文档读取 UWP 上的临时文件(读写)。但是我无法使用下面 link
示例中的文档使其工作Documentation 在这部分:
async void WriteTimestamp()
{
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter =
new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt",
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTimeOffset.Now));
}
但很快我就遇到了“temporaryFolder”的问题。据说在当前上下文中不存在。所以... 我尝试:(1) 添加“使用 Windows.Storage;”不工作... (2) 对外声明:
public Windows.Storage.StorageFolder temporaryFolder { get; set; }
这次代码说没有错误,但是当我启动程序时它停止了。 " 'Object reference not set to an instance of an object.' App12UIWindows.Page2.temporaryFolder.get return 空。
所以我从这里 (Whosebug) 尝试了一个解决方案并且它有效。 Link: Alternative Solution
我的问题是:为什么会出错?文档是正确的还是其他? 还是我在错误的地方搜索?我的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using Windows.Storage;
using System.Threading.Tasks;
namespace App12UIWindows
{
/// <summary>
/// </summary>
public sealed partial class Page2 : Page
{
public Page2()
{
this.InitializeComponent();
SaveTempFunc();
readMyFileTemp();
}
public Windows.Storage.StorageFolder temporaryFolder { get; set; }
async void SaveTempFunc()
{
StorageFile salvandoClientesT = await
temporaryFolder.CreateFileAsync("textOf_Information.txt") ;
await FileIO.WriteTextAsync(salvandoClientesT,"test .. just like that... and
anothers words to a file");
}
//Fim salvando memória temporária
//Lendo na memória temporária arquivo de texto
async void readMyFileTemp()
{
try
{
StorageFile lendoTextoSalvo = await
temporaryFolder.GetFileAsync("textOf_Information.txt");
String timestamp = await FileIO.ReadTextAsync(lendoTextoSalvo);
}
catch (Exception)
{
}
}
}
}
抱歉英语不好:)
'Object reference not set to an instance of an object.' App12UIWindows.Page2.temporaryFolder.get return null."
问题是您只是声明了 temporaryFolder
,但没有应用对 temporaryFolder
的引用。请参考上面提到的document。将 ApplicationData.Current.TemporaryFolder
传递给临时文件夹变量。
public Windows.Storage.StorageFolder temporaryFolder { get; set; } = ApplicationData.Current.TemporaryFolder;