Pass Excel File Path from one class to another adds extra backslashes

I have a variable in my main c# program (note it is using the @):
string xlsTemplate = @"\\server\dir1\subdir2\Test.xls";

Open in new window


I am passing it to a class called "ExcelFile" like this:
ExcelFile excelFile = new ExcelFile();
excelFile.ExcelFilePath = xlsTemplate;

Open in new window


In ExcelFile.cs, I have this:
private string excelFilePath = string.Empty;
public string ExcelFilePath
        {
            get { return excelFilePath; }
            set { excelFilePath = value; }
        }

Open in new window


After I pass it the ExcelFile.cs class thinks the excelFilePath variable looks like this:
"\\\\server\\dir1\\subdir2\\Test.xls"

How can I modify the set, so the original path is preserved without the extra backslashes?
maverick0728Asked:
Who is Participating?
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.

it_saigeDeveloperCommented:
This is normal behaviour because '\' is an escape character.  What is it you need to remove the additional backslashes for?

-saige-
0
maverick0728Author Commented:
I end up trying to open the excel file in ExcelFile.cs with this method.  It errors out because the set using the passed in value has extra backslashes:

public void openExcel()
        {
            myExcelApplication = null;

            myExcelApplication = new Excel.Application(); // create Excell App
            myExcelApplication.DisplayAlerts = false; // turn off alerts
            
              myExcelWorkbook = (Excel.Workbook)(myExcelApplication.Workbooks._Open(excelFilePath, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value)); // open the existing excel file

            myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.Worksheets[1]; // define in which worksheet, do you want to add data
            
        }

Open in new window

0
it_saigeDeveloperCommented:
Try using the normal Open method; e.g. -
using Microsoft.Office.Interop.Excel;
using System;

namespace EE_Q29096715
{
    class Program
    {
        static void Main(string[] args)
        {
            var excel = "C:\\_admin\\test.xls";
            Application application = new Application();
            Workbook workbook = application.Workbooks.Open(excel);
            Worksheet worksheet = workbook.Worksheets[1];
            Console.WriteLine($"Worksheet has {worksheet.UsedRange.Columns.Count} columns and {worksheet.UsedRange.Cells.Count} cells...");
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following output -Capture.PNGUsing the following spreadsheet -Capture.PNGNotice that my code did not change the path to a string literal.  Most likely, your issue is the usage of a network path.

-saige-
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
Tapan PattanaikSenior EngineerCommented:
Hi Maverick,

I believe Microsoft.Office.Interop.Excel namespace is not in the code.

Read Excel File in C#:

https://coderwall.com/p/app3ya/read-excel-file-in-c

Read Excel Files Using Microsoft Office Interop Assemblies in ASP.NET C#:

https://social.technet.microsoft.com/wiki/contents/articles/34906.read-excel-files-using-microsoft-office-interop-assemblies-in-asp-net-c.aspx
0
maverick0728Author Commented:
I am using Microsoft.Office.Interop.Excel namespace in the code.

I want a separate class that handles opening, writing to Excel and closing of Excel.  So how can I pass a network path to this class?
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.