Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Unrecognized Escape Sequence Error

Posted on 2016-10-02
5
Medium Priority
?
153 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 84

Accepted Solution

by:
David Johnson, CD, MVP earned 1200 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 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 84

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

877 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