简体   繁体   中英

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

Morning friends, I wanted to save a datatable in a json, the reason is that I use the data from the json file to a xtraReport which source is the json file. When I run in visual studio 2015 it loaded it and even when I go to the debug folder as well. However, when I create the install using installshield 2015 limited edition (I add the file in the installer) I install it and it creates the application's folder into Program Files(x86) path. When I run the winform application It can't read the json file. The only solution that I found is to execute my winform application as an administrator user. This is my code:

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

when it works (running as an admnistrator): 在此处输入图片说明 When get an exception about denied access (running without specific user) 在此处输入图片说明

I ll apreciate your help. Thanks in advance.

If the dataBar.json file needs to be shared by multiple users, it might be better to have it install to C:\\ProgramData\\SolBienes\\Datos\\dataBar.json. InstallShield should show a folder named "All user program data" or something similar as a target and allow you to set the NTFS permissions for that file.

In C#, you would refer to the file using something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM