如何在程序文件(x86)安装文件夹winform应用程序中写入json文件?

时间:2019-07-19 15:48:56

标签: c# json winforms xtrareport

早上好,我想将数据表保存在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();
}

在运行时(以管理员身份运行): enter image description here 当获得有关拒绝访问的异常(没有特定用户的情况下运行) enter image description here

我会很感激您的帮助。预先感谢。

1 个答案:

答案 0 :(得分:1)

如果dataBar.json文件需要由多个用户共享,最好将其安装到C:\ ProgramData \ SolBienes \ Datos \ dataBar.json。 InstallShield应该显示一个名为“所有用户程序数据”或与目标类似的文件夹,并允许您设置该文件的NTFS权限。

在C#中,您将使用类似以下内容的文件:

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