C# - How read Excel File from INI file path.

Hi,

I am doing some calculation based on my excel input.

This Excel user can select from the particular path and then select the file.

But, now client wants, they don't want to select excel file from the path.

Instead of that, they will mention the file path in the INI file.

While loading the form, first it should read the INI File and then read and convert into datatable.

I am new to use INI File.

Can anyone suggest how to write code to read the file from INI and convert those file into datatable

See my current code below..

 private void btnbrowse_Click(object sender, EventArgs e)
  {
            this.openFileDialog1.Filter = "Excel file (*.xls;)|*.xlsx";
            this.openFileDialog1.Multiselect = true;
            this.openFileDialog1.Title = "Select Excel";
            DialogResult dr = this.openFileDialog1.ShowDialog();
            DataTable dataTable1 = new DataTable();
            
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                foreach (String file in openFileDialog1.FileNames)
                {
                    Excelname = file;
                    txtXLfile.Text = Excelname;
                }
            }
            dtproject = getXLDatatable(Excelname, dataTable1);

            DataRow dr1;

            dr1 = dtproject.NewRow();
            dr1.ItemArray = new object[] { 0, "Select Project" };
            dtproject.Rows.InsertAt(dr1, 0);
            cmbProject.ValueMember  = "EmployeeName";
            cmbProject.DisplayMember = "EmployeeName";
            cmbProject.DataSource = dtproject;  
    }

    private static DataTable getXLDatatable(string XLbroname, DataTable dataTable1)
        {
           
            FileStream fstream1 = new FileStream(XLbroname, FileMode.Open);
          
            Workbook workbook1 = new Workbook(fstream1);
            Worksheet worksheet1 = workbook1.Worksheets[0];
            dataTable1.AcceptChanges();
            DataSet ds1 = new DataSet();
            DataTable dt2 = new DataTable();
            ds1.Tables.Add(dataTable1);
            dataTable1 = worksheet1.Cells.ExportDataTable(1, 0, worksheet1.Cells.MaxRow + 1, worksheet1.Cells.MaxColumn + 1);
            dataTable1.TableName = "tableXL";
            dataTable1.Columns[0].ColumnName = "Id";
            dataTable1.Columns[1].ColumnName = "EmployeeName";
            dataTable1.Columns[2].ColumnName = "Dept";
            dataTable1.Columns[3].ColumnName = "Section";

            dt2 = dataTable1;
            return dt2;
        }

Open in new window


Inside the INI File path something like may be in the serverpath or localpath.
Ganesh STech Lead cum developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Step 1: Why on earth ini files instead of XML?

Step 2: NuGet e.g. INI Parser.

Step 3: I strongly recommend using simply XML instead.

[SomeSection]
SomeFilePath=C:\Some\Path\File.xlsb

Open in new window


vs.

<Section Key="SomeSection">
  <File Key="SomeFilePath" Path="C:\Some\Path\File.xlsb" />
</Section>

Open in new window


And XML Support is built-in.

Can anyone suggest how to write code to read the file from INI and convert those file into datatable
Please cleanup you code first. You're mangling UI, file system interaction, data handling in single procedures.
0
Ganesh STech Lead cum developerAuthor Commented:
we don't want to use excel file due to some secured purpose and also user should not know where this file has kept in.

due to this, we converted into ini and user simply loading the apps without knowing any secured information...
0
ste5anSenior DeveloperCommented:
XML. Not Excel.

Your security concerns about knowledge on the user-side is independent of the storage type of that information.

Well, though it was clear: XML is simpler in .NET. INI files are far more complex.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ganesh STech Lead cum developerAuthor Commented:
can you pls. provide some sample code with same logic...?
0
ste5anSenior DeveloperCommented:
E.g.

namespace ConsoleCS
{
    using System;
    using System.Xml.Linq;
    using System.Xml.XPath;

    public class Program
    {
        public static void Main(string[] args)
        {
            XDocument configuration;
            configuration = XDocument.Load(@"C:\Temp\test.xml");
            foreach (XElement file in configuration.XPathSelectElements("//Section[@Key='SomeSection']/File"))
            {
                Console.WriteLine("File: {0}, {1}", file.Attribute("Key").Value, file.Attribute("Path").Value);
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Open in new window


with the above already posted XML stored at C:\Temp\test.xml.
0
Ganesh STech Lead cum developerAuthor Commented:
Thanks for your update.

I have two concerns.

I want to keep and maintain INI file in network folder/File server folder.
ExcelPath file (excelfile) also keep and maintain in network folder/File server folder.
ExcelPath : for example (\\server201\Project1\documents\projects.xlsx)

INI File ==> still keep maintaining application running folder.

In that case how do we mention server path for ini file and Excel file....?

Note : Application (.EXE) is running from network folder. The reason is to keep maintain INI File in folder, user should not see the file path and admin only will maintain this file
0
ste5anSenior DeveloperCommented:
As XML is as flexible as an ini. You can do, whatever you want:

<Section Key="SomeSection">
  <File Key="ExcelPath" Path="\\server201\Project1\documents\projects.xlsx" />
  <File Key="SomeTextFile" Path="\\server201\Documents\ATextFile.txt" />
</Section>

Open in new window


But I have some troubles understanding your actual, concrete situation (context).

Note : Application (.EXE) is running from network folder. The reason is to keep maintain INI File in folder, user should not see the file path and admin only will maintain this file
This is not possible. When the application can read it, the user can read it also. There is no security gain here.
You can separate usage from maintenance, but this means setting NTFS/share permissions. But this still means the user must be able to read the file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ganesh STech Lead cum developerAuthor Commented:
instead of XML,

can you pls. provide the ini code..?
0
Ganesh STech Lead cum developerAuthor Commented:
thanks.it's working.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.