0
我正在创建一个需要保存永久数据的桌面应用程序。我能想到的最简单的方法是将关于该文件的重要信息写入文本文件。为了使数据包含,专业且易于访问的应用程序,我决定把它放在用户计算机的“程序文件”目录中的新创建的文件夹中。不幸的是,这被计算机阻止。它拒绝访问我的应用程序。我知道我的代码有效,因为我以管理员身份运行该应用程序并且没有遇到任何错误它还根据我的需要在“程序文件”中创建了该文件夹。无论如何都允许访问。我也想先获得用户的许可。代码如下发布:保存程序文件中的永久数据的C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyAppNameSpace
{
static class DataManager
{
/* Create a dictionary that takes type "string" for its
* key (name) and type "object" for its value */
public static Dictionary<string, object> Data = new Dictionary<string, object>();
public static string FileName { get; private set; }
public static string ProgramFilesFolder;
// A method to clear all game data
public static void Clear()
{
}
public static void Save()
{
foreach (KeyValuePair<string, object> DataPair in Data)
{
}
}
public static string GetData()
{
return File.ReadAllText(FileName);
}
public static void Setup()
{
ProgramFilesFolder = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "/MyAppName";
FileName = ProgramFilesFolder + "/MyAppData";
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "/MyAppName/README.txt"))
{
try
{
Directory.CreateDirectory(ProgramFilesFolder);
string text = "DO NOT move this folder. If you must, make sure to keep it " +
"in the same location as the SwingState it is. Also, do not edit the " +
"contents of the data file, which exists in this directory. " +
"This saves all of your progress in your game!";
File.WriteAllText(ProgramFilesFolder + "/README.txt", text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ":" + ex.InnerException);
GameInfo.Game.Close();
}
}
if (!File.Exists(FileName))
{
Data = new Dictionary<string, object>()
{
{ "Name:", null + "," },
{ "Age:", null + "," },
{ "Description:", null + "," },
{ "Gender:", null + "," },
{ "Level:", 1 + "," },
{ "Drawing:", false + "," },
{ "Uploaded:", false + "," },
{ "Template:", false + "," },
{ "Left1:", null + "," },
{ "Left2:", null + "," },
{ "Left3:", null + "," },
{ "Right1:", null + "," },
{ "Right2:", null + "," },
{ "Right3:", null + "," },
{ "Idle:", null + "," },
{ "Template Number:", null + "," }
};
}
else if (File.Exists(FileName))
{
string[] DataText = File.ReadAllText(FileName).Split(',');
foreach (string s in DataText)
{
Data.Add(s.Split(':')[0], s.Split(':')[1]);
}
}
}
}
}
AppData文件夹是应用程序数据的常见位置。 – Crowcoder
我不相信这会给我priveleges,无论。 – Akinni
这就是文件夹存在的原因,你试过了吗? – Crowcoder