I am developing a WFA that is used as a database based on an excel document (.xlsx), the content of the document provides the database of information with which the user works, currently the program works without any problem when the user manually loads the excel file , but it is necessary that the user has no interaction with the file, so I have tried to initialize the document automatic way When I try that, Visual Studio tells me the following error:
"System.ObjectDisposedException: 'Can not access a disposed object."
The document is in a folder inside another, which in turn are in the same folder that contains the .exe.
I tried to do the following:
string path = Application.StartupPath;
string file = @"Resources\db\tabla.xlsx";
string full = Path.Combine(path,file);
The result of full gives me the following:
C: \ User \ Documents \ Solution \ Bin \ Debug \ Resources \ db \ table.xlsx
where
C: \ user \ documents \ solution \
is the location of the folder that contains the .exe and
Resources \ db \ table.xlsx
is the location of the file I'm trying to load.
I enclose the corresponding code fragments in the manual and automatic way with which I upload the excel file:
Manual
Using system.reflection;
Using Excel = Microsoft.office.interop.excel;
Public partial class Form1 : Form
{
Excel.application oXL;
Excel._woorkbook oWB;
Excel._worksheet oST;
Object misvalue = system.reflection.missing.value;
Public Form1
{
InitializeComponent();
Openfiledialog open = new openfiledialog();
Open.restoredirectory = true;
Messagebox.show("Selecciona la base de datos a usar");
If (open.showdialog() != Dialogresult.cancel)
{
Try
{
oXL= new Excel.application();
oXL.visible = false;
oXL.Usercontrol = true;
oWB = (oXL.workbooks.open(FileName));
oST =
(Excel._worksheet)oWB.sheets.get_Item("sheet1");
Etc.
}
Catch (exception)
{
Messagebox.show("ocurrio un error al cargar la base de datos");
oXL.quit();
This.close();
}
}
}
}
Automatic
Using system.reflection;
Using Excel = Microsoft.office.interop.excel;
Public partial class Form1 : Form
{
Excel.application oXL;
Excel._woorkbook oWB;
Excel._worksheet oST;
Object misvalue = system.reflection.missing.value;
Public Form1
{
InitializeComponent();
String path=application.startuppath;
String file = @"Resources\db\tabla.xlsx";
String full=path.combine(path,file);
Try
{
oXL= new Excel.application();
oXL.visible = false;
oXL.Usercontrol = true;
oWB = oXL.workbooks.open(full);
oST =
(Excel._worksheet)oWB.sheets.get_Item("sheet1");
Etc.
}
Catch (exception)
{
Messagebox.show("ocurrio un error al cargar la base de datos");
oXL.quit();
This.close();
}
}
}
I have 2 questions about this:
1- How is the correct way to make a file contained in the folders corresponding to the root of the .exe folder run?
2- What does it imply that the aforementioned error jumps? (I noticed that it is due to the inability to upload the excel document)