在给定文件夹中创建文本文件
我正在开发使用Microsoft Visual Studio 2013的Windows 8应用程序。我需要将用户输入的数据存储在文本文件中。我写了下面的代码段来创建文件及其工作。但是文本文件是在C:\ Users中创建的......我想在给定文件夹中创建文本文件。如何修改我的代码以在指定的文件夹中创建文件。在给定文件夹中创建文本文件
StorageFile sampleFile;
const string fileName = "Sample.txt";
这是你如何C语言创建的临时文件夹
String folderPath = @"C:/temp";
FileStream fs = new FileStream(folderPath + "\\Samplee.txt",FileMode.OpenOrCreate, FileAccess.Write);
'C/temp'!='C:/ temp ' – leppie 2014-11-14 12:23:41
@leppie,更正了它 – Pankaj 2014-11-14 12:24:26
我不认为这是正确的答案。他想使用类[StorageClass](http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.aspx?cs-save-lang=1&cs-lang=csharp#代码片段1)的Windows应用商店应用程序。 StorageClass适用于Windows 8和Windows Phone 8应用程序。 – 2014-11-14 12:36:04
文件您需要设置要保存文件的目录。
试试这个
string dirctory = @"D:\Folder Name"; //This is the location where you want to save the file
if (!Directory.Exists(dirctory))
{
Directory.CreateDirectory(dirctory);
}
File.WriteAllText(Path.Combine(dirctory, "Sample.txt"), "Text you want to Insert");
我有导入System.IO命名空间,但目录下划线为错误。 – KMA 2014-11-15 04:07:09
如前告诉记者,通用应用程序是沙箱,这意味着你不能在任意文件夹写入文件。
你应该看看File access sample如何做到这一点。
此外,您应该看看ApplicationData,它为您提供了很多选择来保存用户输入的数据。这是暂时的吗,你想让它同步吗,它是一个设置吗?肯定有一个适合您需要的财产。
编辑:从http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localfolder.aspx这是你应该通过“Windows 8应用”做什么
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;
// Write data to a file
function writeTimestamp() {
localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (sampleFile) {
var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
var timestamp = formatter.format(new Date());
return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
}).done(function() {
});
}
你的意思是“沉浸式” /“地铁”型应用程序?这些应用程序是沙盒式的,不能写入文件系统中的任何任意目录。我建议以下这篇文章:http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html – Dai 2014-11-14 12:24:05