C# String and Escape sequence during Concatentation???

D

dm1608

I'm looping thru a reader object for SQL Server. Two of the files I'm
returning are the "UNC" name of a server and a "File Path". Data looks
something like:

UNC = \\10.246.16.18\MYSHARE
File Path = "\MyDir\MySub\myfile.txt"

I'm trying to append these two fields to a string and I'm only getting the
UNC. Looking at a watch window, it looks like perhaps the backslashes are
messing me up.

I'm seeing something like:

UNC = \\\\10.246.16.18\\MYSHARE
File Path = \\MyDir\\MySub\\myfile.txt

I'm trying to assign the values such as:

string myoutput = "File " +
Convert.ToString(reader.GetValue(reader.GetOrdinal("lastuseddrive"))) + "\\"
+ Convert.ToString(reader.GetValue(reader.GetOrdinal("filepath"))) ;

Of course, myoutput only contains the "lastuseddrive" value. I can see the
"filepath" value in the watch window.

I've tried prefixing the @ sign in front of my quotes and didn't see any
difference?

Any help would be appreciated.
 
D

dm1608

I think the issue is that my "FilePath" string directory name is "\v5" and
the \v is being escaped. How do I disable this functionality so I can see
the literal?
 
J

Jon Skeet [C# MVP]

dm1608 said:
I'm looping thru a reader object for SQL Server. Two of the files I'm
returning are the "UNC" name of a server and a "File Path". Data looks
something like:

UNC = \\10.246.16.18\MYSHARE
File Path = "\MyDir\MySub\myfile.txt"

I'm trying to append these two fields to a string and I'm only getting the
UNC. Looking at a watch window, it looks like perhaps the backslashes are
messing me up.

See http://www.pobox.com/~skeet/csharp/strings.html and in particular
the bit about the debugger.
 
C

Chris Dunaway

dm1608 said:
I'm trying to assign the values such as:

string myoutput = "File " +
Convert.ToString(reader.GetValue(reader.GetOrdinal("lastuseddrive"))) + "\\"
+ Convert.ToString(reader.GetValue(reader.GetOrdinal("filepath"))) ;

You can shorten that line to this:

string myoutput = "File " + reader.GetString(.....) + "\\" +
reader.GetString(....);

Also, the GetOrdinal function add a bit of overhead. What we have done
that works well is to create a class with constants that indicate the
layout of the db. Then you can remove the GetOrdinal call altogether:

public class TableLayout]
{
public const int LastUsedDrive = 0;
public const int FilePath = 1;
//etc
}

Then you can call it like this:

TableLayout tl = new TableLayout();

string myoutput = "File " + reader.GetString(tl.LastUsedDrive) + "\\" +
reader.GetString(tl.FilePath);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top