The two backslashes are required to escape the single backslash character because the backslash has special meaning within a string. Use the literal "@" if you want a single backslash in the code.
string s = @"test\MoreTesting";
A side from string basics. Your code is invalid and does not compile.
using System;
namespace ConsoleDemo
{
class Program
{
public static void Main()
{
var v1 = "a\\b"; // debugger shows a\\b
var v2 = @"a\b"; // debugger shows a\\b
Console.WriteLine(v1==v2); // console shows True
Console.WriteLine(v1); // console shows a\b
}
}
}
Get back at the exception and if you can try to dump ex.ToString() to get the full exception chain and stack rather than maybe ex.Message as you are perhaps doing now...
i am passing a ssis variable contains a excel filepath. = \\server.com\folder1\folder2\excelFile_2020.xlsx
when i debug the c# my filepath becomes = \\\\server.com\\folder1\\folder2\\excelFile_2020.xlsx
I don't know why there are 2 more backslashes in front of your file path, but if you want to get the absolute path for the specified path string, you can try to use the Path.GetFull Path Method.
If you still have the question, please post your code.
Best regards,
Sam
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
35 Points
375 Posts
Replace two backslashes with one Not working
Feb 25, 2020 01:58 PM|masterdineen|LINK
Hello there.
This is quite a simple issue or should be.
All i want to do within my code is replace "\\" with "\"
see example below.
but the string does not change.
can someone help me please.
All-Star
53101 Points
23659 Posts
Re: Replace two backslashes with one Not working
Feb 25, 2020 02:09 PM|mgebhard|LINK
The two backslashes are required to escape the single backslash character because the backslash has special meaning within a string. Use the literal "@" if you want a single backslash in the code.
A side from string basics. Your code is invalid and does not compile.
Member
35 Points
375 Posts
Re: Replace two backslashes with one Not working
Feb 25, 2020 02:13 PM|masterdineen|LINK
Thank you for getting back to quickly.
What about taking a SSIS variable that has a filepath.
for example the SSIS variable would be
\\server.com\folder1\folder2\excelFile_2020.xlsx
All-Star
53101 Points
23659 Posts
Re: Replace two backslashes with one Not working
Feb 25, 2020 03:19 PM|mgebhard|LINK
I'm not sure what you are asking. Strings behave as written in the C# programming guide.
Are you receiving an error message? If so, what is the error message?
Member
35 Points
375 Posts
Re: Replace two backslashes with one Not working
Feb 25, 2020 03:47 PM|masterdineen|LINK
i have a c# script task in my ssis package,
i am passing a ssis variable contains a excel filepath. = \\server.com\folder1\folder2\excelFile_2020.xlsx
when i debug the c# my filepath becomes = \\\\server.com\\folder1\\folder2\\excelFile_2020.xlsx
then i get an execption error ( Exception has been thrown by the target of an invocation )
i assume this is because of the extra backslashes,
All-Star
48570 Points
18082 Posts
Re: Replace two backslashes with one Not working
Feb 25, 2020 04:17 PM|PatriceSc|LINK
Hi,
First the C# debugger is showing strings using the C# syntax. \ is used to introduce an escape sequence and needs to be escaped (or you can use the @ prefix). See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim or :
Get back at the exception and if you can try to dump ex.ToString() to get the full exception chain and stack rather than maybe ex.Message as you are perhaps doing now...
Contributor
3370 Points
1409 Posts
Re: Replace two backslashes with one Not working
Feb 26, 2020 05:22 AM|samwu|LINK
Hi masterdineen,
I don't know why there are 2 more backslashes in front of your file path, but if you want to get the absolute path for the specified path string, you can try to use the Path.GetFull Path Method.
More information about Path.GetFullPath you can refer to this link: https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath?view=netframework-4.8
If you still have the question, please post your code.
Best regards,
Sam
Member
35 Points
375 Posts
Re: Replace two backslashes with one Not working
Feb 26, 2020 06:31 AM|masterdineen|LINK
C# requires an escape backlash for each “\” to display unless there is a @ at the
beginning. My file path is coming from a SSIS variable.
so i shall try your solution.
Regards
Rob