将文件从应用程序安装文件夹复制到本地存储
问题描述:
我试图将文件从我的Windows 8应用程序的安装位置复制到本地存储。我一直在研究,试图做到这一点无济于事。这是我到目前为止所提出的,但我不确定我出错的地方。将文件从应用程序安装文件夹复制到本地存储
private async void TransferToStorage()
{
try
{
// Get file from appx install folder
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
StorageFile temp1 = await installedLocation.GetFileAsync("track.xml");
// Read the file
var lines = await FileIO.ReadLinesAsync(temp1);
//Create the file in local storage
StorageFile myStorageFile = await localFolder.CreateFileAsync("track_iso.xml", CreationCollisionOption.ReplaceExisting);
// Write to it
await FileIO.WriteLinesAsync(myStorageFile, lines);
}
catch (Exception)
{
}
}
任何想法?
答
自己解决。这里是遇到这个问题/问题的其他人的方法:
private async void TransferToStorage()
{
// Has the file been copied already?
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync("localfile.xml");
// No exception means it exists
return;
}
catch (System.IO.FileNotFoundException)
{
// The file obviously doesn't exist
}
// Cant await inside catch, but this works anyway
StorageFile stopfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///installfile.xml"));
await stopfile.CopyAsync(ApplicationData.Current.LocalFolder);
}
只是好奇,但是需要这样做的目的是什么? – 2015-08-18 01:23:37
@ A.R。例如我有一个XML配置文件模板,我想将其转移到本地存储,以便我可以修改它。 – ForeverLearning 2015-09-13 06:43:45
哦,我忘了更新/添加评论。在问我的问题后,我很快就发现了我为什么想要。我有一个默认设置文件,包含在安装中,然后复制到本地以保存新设置等。 – 2015-09-14 20:50:38