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"));