C# 在给定文件夹中创建文本文件

C# 在给定文件夹中创建文本文件,c#,windows,windows-phone-8,file-handling,C#,Windows,Windows Phone 8,File Handling,我正在使用Microsoft visual studio 2013开发windows 8应用程序。我需要将用户输入的数据存储在文本文件中。我编写了以下代码段来创建文件及其工作机制。但是文本文件是在C:\Users中创建的。。。。。。我想在给定文件夹中创建文本文件。如何修改代码以在指定的文件夹中创建文件 StorageFile sampleFile; const string fileName = "Sample.txt"; 这就是如何在C temp文件夹中创建文件的方法 String fold

我正在使用Microsoft visual studio 2013开发windows 8应用程序。我需要将用户输入的数据存储在文本文件中。我编写了以下代码段来创建文件及其工作机制。但是文本文件是在C:\Users中创建的。。。。。。我想在给定文件夹中创建文本文件。如何修改代码以在指定的文件夹中创建文件

StorageFile sampleFile;
const string fileName = "Sample.txt";

这就是如何在C temp文件夹中创建文件的方法

String folderPath = @"C:/temp";
FileStream fs = new FileStream(folderPath + "\\Samplee.txt",FileMode.OpenOrCreate, FileAccess.Write);

这就是如何在C temp文件夹中创建文件的方法

String folderPath = @"C:/temp";
FileStream fs = new FileStream(folderPath + "\\Samplee.txt",FileMode.OpenOrCreate, FileAccess.Write);

您需要设置保存文件的目录

试试这个

       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");

您需要设置保存文件的目录

试试这个

       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");

如前所述,通用应用程序是沙盒式的,这意味着您不能在任意文件夹中写入文件

你应该看看关于如何做这件事的指南

此外,您还应该查看,它为您提供了许多保存用户输入数据的选择。它是临时的吗?你想同步吗?它是设置吗?确实有一处房产适合你的需要

编辑:这是您应该做的

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 () {      
      });
}

如前所述,通用应用程序是沙盒式的,这意味着您不能在任意文件夹中写入文件

你应该看看关于如何做这件事的指南

此外,您还应该查看,它为您提供了许多保存用户输入数据的选择。它是临时的吗?你想同步吗?它是设置吗?确实有一处房产适合你的需要

编辑:这是您应该做的

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 () {      
      });
}

C/temp
!=<代码>C:/temp@leppie,更正了。我认为这不是正确答案。他想将该类用于Windows应用商店应用程序。StorageClass适用于Windows 8和Windows Phone 8应用程序。FileStream在我的程序代码中突出显示为错误。但是我使用的是System.IO名称空间。
C/temp
!=<代码>C:/temp@leppie,更正了。我认为这不是正确答案。他想将该类用于Windows应用商店应用程序。StorageClass适用于Windows 8和Windows Phone 8应用程序。FileStream在我的程序代码中突出显示为错误。但是我使用的是System.IO名称空间。你说的“Windows 8应用程序”是指“沉浸式”/“Metro”类型的应用程序吗?这些应用程序是沙盒,不能写入文件系统中的任意目录。我建议如下:你所说的“Windows8应用程序”是指“沉浸式”/“Metro”类型的应用程序吗?这些应用程序是沙盒,不能写入文件系统中的任意目录。我建议阅读本文:我有import System.IO名称空间,但目录下划线为错误。我有import System.IO名称空间,但目录下划线为错误。