Page 1 of 1

How to set relative path of excel file in Usercode.cs?

Posted: Fri Feb 03, 2017 6:18 am
by Madhumita Ghosh3
Hi,

I have gone through the posts in this forum which states how to set relative path of excel file in the 'Manage Data Sources' part by checking the 'Copy file to project' checkbox. I have done the same in my testcase. Now I want to set the relative path of the same excel file in my usercode as I need to write some values in my excel sheet from web application. Please follow the code which am using for writing in excel file:

var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Open("C:/Work/Automation/TowCall_D3.xlsx");
Excel._Worksheet worksheet = (Excel.Worksheet)excelApp.ActiveSheet;
worksheet.Cells[2,"AS"] = strcallID;
excelApp.ActiveWorkbook.Save();
excelApp.Quit();

For the line where am setting the excel file path I want to use a relative path just like it happens in 'Manage Data Sources' part by checking the 'Copy file to project' checkbox.

Note: I want to use a relative path because the scripts would be run in different machines and hence users need to create a folder in their local machine (C:/Work/Automation/TowCall_D3.xlsx). Also replace the excel everytime it is updated by 1 user.

Please let me know the solution for this. Thanks in advance.

Re: How to set relative path of excel file in Usercode.cs?

Posted: Mon Feb 06, 2017 8:43 am
by Madhumita Ghosh3
Please let me know if there is any solution to my above stated problem. Looking forward to your reply.
Thanks in advance.

Re: How to set relative path of excel file in Usercode.cs?

Posted: Mon Feb 06, 2017 9:08 am
by odklizec
Hi,

A relative path to what...the test exe? In this case, you can use something like this:

Code: Select all

string getActualDir = TestSuite.WorkingDirectory.ToString(); 
excelApp.Workbooks.Open(getActualDir  + "\\TowCall_D3.xlsx");
...
Hope this helps?

BTW, users should not be needed to create anything manually! I would strongly suggest to use a proper version control system, to distribute your tests (ideally combined with continuous integration system, like Jenkins, Bamboo, etc...). Copying files manually is a certain way to mess and confusion ;)