如何写入位于 Program files (x86) 安装文件夹 winform 应用程序中的 json 文件?

How to write in a json file located into the Program files (x86) installation folder winform application?

早上好朋友们,我想在json中保存一个数据表,原因是我使用json文件中的数据到xtraReport,其中源是json文件。当我在 visual studio 2015 年 运行 时它加载了它,甚至当我进入调试文件夹时也是如此。但是,当我使用 installshield 2015 限量版(我在安装程序中添加文件)创建安装时,我安装它并将应用程序的文件夹创建到 Program Files(x86) 路径中。当我 运行 winform 应用程序无法读取 json 文件。我找到的唯一解决方案是以管理员用户身份执行我的 winform 应用程序。这是我的代码:

public FrmDocBien()
{
    InitializeComponent();
    dt = new DataTable();
    dt.Clear();
    dt.Columns.Add("IDB");
    dt.Columns.Add("DATEB");
    dt.Columns.Add("BARCODE");  
}
public void DataTableToJSONWithStringBuilder(DataTable table)
{
    var JSONString = new StringBuilder();
    if (table.Rows.Count > 0)
    {
        JSONString.Append("[");
        for (int i = 0; i < table.Rows.Count; i++)
        {
            JSONString.Append("{");
            for (int j = 0; j < table.Columns.Count; j++)
            {
                if (j < table.Columns.Count - 1)
                {
                    JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
                }
                else if (j == table.Columns.Count - 1)
                {
                    JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
                }
            }
            if (i == table.Rows.Count - 1)
            {
                JSONString.Append("}");
            }
            else
            {
                JSONString.Append("},");
            }
        }
        JSONString.Append("]");
    }
    System.IO.File.WriteAllText((Application.StartupPath + "\dataBar.json").Replace("\bin\Debug", ""), JSONString.ToString());
}
private void btnLoadxtraReport_Click_1(object sender, EventArgs e)
{
    this.DataTableToJSONWithStringBuilder(dt);
    rpBar = new xrDemo();
    rpBar.CreateDocument();
    pt = new ReportPrintTool(rpBar);
    pt.ShowPreview();
}

何时可用(运行作为管理员): 当获得关于拒绝访问的异常时(运行ning 没有特定用户)

非常感谢您的帮助。提前致谢。

如果 dataBar.json 文件需要由多个用户共享,最好将其安装到 C:\ProgramData\SolBienes\Datos\dataBar.json。 InstallShield 应显示名为 "All user program data" 或类似名称的文件夹作为目标,并允许您为该文件设置 NTFS 权限。

在 C# 中,您可以使用如下方式引用文件:

string dataBarFilePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"SolBienes\Datos\dataBar.json"));