Solved

C# Unrecognized Escape Sequence Error

Posted on 2016-10-02
5
47 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
  • 2
  • 2
5 Comments
 
LVL 78

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 78

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now