本文介绍了将文件保存到我的文档+ app文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想挽救我的.NET应用程序的设置文件到用户的%myDocument中%\\ MyApplication的文件夹,但我不知道如何检查现有文件夹\\文件,以及创建或追加在保存文件夹\\文件。我不想打开saveFileDialog,因为我需要的文件是在所有用户计算机上的相同位置。这是我有这么远,但它不工作。任何帮助将是AP preciated:
VAR saveSettings = settingsList.text; //设置分配给一个变量
saveSettings = Regex.Replace(saveSettings,@\\ S +,).Trim()+ Environment.NewLine; //删除任何多余的空格,并添加一个回车,使每个设置是一个新行变种文件名=的String.Format({0} \\\\ {1},Environment.SpecialFolder.MyDocuments +\\\\ \\\\的MyApp,settings.dat); //生成路径settings.dat
File.AppendAllText(文件名,saveSettings); //保存settings.dat
解决方案
路径字符串= System.IO.Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.MyDoc uments),MyApp的,settings.dat);如果(Directory.Exists(路径))
{
//存在
}
其他
{
//需要创建
}
I am trying to save my .NET application settings file to the user's %MyDocument%\MyApplication folder, but I don't know how to check for an existing folder\file, and create or append the folder\file on save. I don't want to open a saveFileDialog because I need the file to be in the same place on all user computers. This is what I have so far, but it isn't working. Any help would be appreciated:
var saveSettings = settingsList.text; //assign settings to a variable
saveSettings = Regex.Replace(saveSettings, @"\s+", "").Trim() + Environment.NewLine; //remove any extra spaces and add a carriage return so that each setting is on a new line
var fileName = string.Format("{0}\\{1}", Environment.SpecialFolder.MyDocuments + "\\MyApp\\", "settings.dat"); //generate path to settings.dat
File.AppendAllText(fileName, saveSettings); //save settings.dat
解决方案
string path = System.IO.Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments),"MyApp","settings.dat");
if(Directory.Exists(path))
{
//Exists
}
else
{
//Needs to be created
}
这篇关于将文件保存到我的文档+ app文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!