C#中的@符号 [英] @ symbol in C#
问题描述
您好,
我在声明字符串值时没有任何问题,除非涉及
目录路径'。为什么C#隐含地将@符号放在
的字符串值中,这个值已经声明如下:
string strSourceDirectory =" C:\\ \\\Documents and Settings \\admin \\My
Documents \\Excel\\Excel \\" ;;
strSourceDirectory分配的值是:
@" C:\Documents and Settings \admin \ My Documents \Excel\Excel \ ;
我如何绕过这个因为我的程序错误,因为我分配给特定的strSourceDirectory中的
目录路径
我要打开的文件不存在?
这是我得到的真实错误:
@"'' C:\Documents and Settings \admin \ My
Documents\Excel\Excel\TEST200609050.csv''不是有效路径。确保
路径名称拼写正确,并且您已连接到文件所在的服务器
。
谢谢,
SAL写道:
I声明字符串值没有任何问题,除非涉及到
目录路径'。为什么C#隐含地将@符号放在
的字符串值中,这个值已经声明如下:
string strSourceDirectory =" C:\\ \\\Documents and Settings \\admin \\My
Documents \\Excel\\Excel \\" ;;
strSourceDirectory分配的值是:
@" C:\Documents and Settings \admin \ My Documents \Excel\Excel \ ;
C#根本就没有这样做。这就是调试器向你展示的内容。
上面的两个字符串是相同的。
如何解决这个问题因为我的程序错误了,因为strSourceDirectory中的
目录路径我分配给我要打开的特定
文件不存在?
然后目录不存在 - 它与
反斜杠无关。
顺便说一下,你是* *在那里有两次Excel吗?
Jon
>
你好Jon,
是的,我的意思是让Excel在那里两次。我正在进行正确的测试,
目录确实存在。
" Jon Skeet [C#MVP]"写道:
SAL写道:
我声明字符串值没有任何问题,除非它出现到
目录路径''。为什么C#隐含地将@符号放在
的字符串值中,这个值已经声明如下:
string strSourceDirectory =" C:\\ \\\Documents and Settings \\admin \\My
Documents \\Excel\\Excel \\" ;;
strSourceDirectory分配的值是:
@" C:\Documents and Settings \admin \ My Documents \Excel\Excel \ ;
C#根本就没有这样做。这就是调试器向你展示的内容。
上面的两个字符串是相同的。
如何解决这个问题因为我的程序错误了,因为strSourceDirectory中的
目录路径我分配给我要打开的特定
文件不存在?
然后目录不存在 - 它与
反斜杠无关。
顺便说一下,你是* *在那里有两次Excel吗?
Jon
你好Jon,
最后,目录路径将从数据库表中读取而不是
在我的硬编码程序,但是现在我正在测试我已经用这种方式声明了
。我在Windows资源管理器中从地址行做了一个cut-n-paste到
确保我的路径正确,但C#隐含地将@符号
放在前面导致错误的路径。
" Jon Skeet [C#MVP]"写道:
SAL写道:
我声明字符串值没有任何问题,除非它出现到
目录路径''。为什么C#隐含地将@符号放在
的字符串值中,这个值已经声明如下:
string strSourceDirectory =" C:\\ \\\Documents and Settings \\admin \\My
Documents \\Excel\\Excel \\" ;;
strSourceDirectory分配的值是:
@" C:\Documents and Settings \admin \ My Documents \Excel\Excel \ ;
C#根本就没有这样做。这就是调试器向你展示的内容。
上面的两个字符串是相同的。
如何解决这个问题因为我的程序错误了,因为strSourceDirectory中的
目录路径我分配给我要打开的特定
文件不存在?
然后目录不存在 - 它与
反斜杠无关。
顺便说一下,你是* *在那里有两次Excel吗?
Jon
Hello,
I do not have any trouble declaring string values except when it comes to
directory path''s. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
The value that strSourceDirectory gets assigned is:
@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
Here is the acutal error I get:
@"''C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv'' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."
Thanks,
SAL wrote:I do not have any trouble declaring string values except when it comes to
directory path''s. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
The value that strSourceDirectory gets assigned is:
@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"C# isn''t doing that at all. It''s just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?Then the directory doesn''t exist - it''s nothing to do with the
backslashes.
Did you *mean* to have Excel in there twice, by the way?
Jon
Hi Jon,
Yes I meant for Excel to be in there twice. I''m doing testing right and the
directory does exist.
"Jon Skeet [C# MVP]" wrote:
SAL wrote:I do not have any trouble declaring string values except when it comes to
directory path''s. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
The value that strSourceDirectory gets assigned is:
@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
C# isn''t doing that at all. It''s just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
Then the directory doesn''t exist - it''s nothing to do with the
backslashes.
Did you *mean* to have Excel in there twice, by the way?
Jon
Hi Jon,
Eventually, the directory path will be read from a database table instead of
hard-coding in my program, but for now while I''m testing I have it declared
this way. I did a cut-n-paste from the address line in Windows Explorer to
make sure I had the path correct, but C# is implicitly putting the @ symbol
in front of the path causing errors.
"Jon Skeet [C# MVP]" wrote:
SAL wrote:I do not have any trouble declaring string values except when it comes to
directory path''s. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
The value that strSourceDirectory gets assigned is:
@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
C# isn''t doing that at all. It''s just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
Then the directory doesn''t exist - it''s nothing to do with the
backslashes.
Did you *mean* to have Excel in there twice, by the way?
Jon
这篇关于C#中的@符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!