Solved

C# Unrecognized Escape Sequence Error

Posted on 2016-10-02
5
98 Views
Last Modified: 2016-10-02
I’m trying to write some simple C# code in a winform app that gets the same results as a VB.Net sub. Please help me correct the error. The following VB (in a winform app) code runs without any errors:

Dim retval As String
Dim ls_temp As String
ls_temp = "C:\Program Files (x86)\Visual CUT 11\Visual CUT.exe " & _
"-e ""C:\test\Email_Test_1aa.rpt"""
retval = Shell(ls_temp)

The following C# code results in an “Unrecognized escape sequence” error:
string retval;
string ls_temp;
retval = C:\Program Files (x86)\Visual CUT 11\Visual CUT.exe " +
"-e ""C:\test\Email_Test_1aa.rpt""";
0
Comment
Question by:Mark01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 80

Accepted Solution

by:
David Johnson, CD, MVP earned 300 total points
ID: 41825510
Change the single \'s to \\'s to escape the escape sequence
\t = tab character
\r = return character
\n = newline character

  string retval;
            retval = "C:\\Program Files (x86)\\Visual CUT 11\\Visual CUT.exe " + "-e "  + "C:\\test\\Email_Test_1aa.rpt";
            Console.WriteLine(retval);
            Console.ReadLine();

Open in new window

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 41825512
An alternative is to use the @ symbol with the string, this tells the compiler to ignore all escape sequence characters.
@"C:\Program Files (x86)\Visual CUT 11\Visual CUT.exe "  is the same as "C:\\Program Files (x86)\\Visual CUT 11\\Visual CUT.exe "


ps. I'm surprised the VB code worked, there are spaces in the path
0
 

Author Comment

by:Mark01
ID: 41825557
That resolved the error. Can someone give me a hint to the C# equivalent of the following VB code:
        retval = Shell(ls_temp)
0
 
LVL 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 300 total points
ID: 41825570
Long-winded but it works
 Process process = new Process();
        process.StartInfo.FileName = "[program name here]";
        process.StartInfo.Arguments = "[arguments here]";
        process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
        process.Start();
        process.WaitForExit();
        int code = process.ExitCode;

Open in new window


https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
0
 

Author Closing Comment

by:Mark01
ID: 41825593
Thank you, David and Andy.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question