在给定文件夹中创建文本文件
本文关键字:创建 文本 文件 文件夹 | 更新日期: 2023-09-27 18:30:27
我正在使用Visual Studio 2013开发Windows 8应用程序Microsoft。我需要将用户输入的数据存储在文本文件中。我编写了以下代码段来创建文件及其工作。但是文本文件是在 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);
如前所述,通用应用程序是沙盒化的,这意味着您无法在任意文件夹中写入文件。
您应该查看文件访问示例,了解如何执行此操作。
另外,您应该查看应用程序数据,它为您提供了很多保存用户输入数据的选择。它是临时的,是否要同步,是设置吗?肯定有适合您需求的房产。
编辑:从 http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localfolder.aspx 这是你应该做的
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 () {
});
}
您需要设置要保存文件的目录。
试试这个
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");