Solved

C# Unrecognized Escape Sequence Error

Posted on 2016-10-02
5
124 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 82

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 82

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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