0

パスを保存するstringBuilderとstringクラスがあります:

StringBuilder name = new StringBuilder();
name.Append(@"NETWORK\MyComputer");
String serverName = name.ToString();  //this converts the \ to a \\

私は多くのことを試しましたが、常に \ を持つ文字列になります

を使用serverName.Replace("\\", @"\");しても機能せず、\ のままにします

servername.Replace("\\", "\""); は文字列にa を追加し"ますが、これはまだ正しくありません。

手伝ってください。

4

3 に答える 3

7

1 つのバック スラッシュが 2 つのバック スラッシュとして表示されることに懸念がある場合は、気にしないでください。これは、デバッガーで表示される方法です。

バックスラッシュは特殊文字で、2 重にすることで「特殊性」が無効になります。または、@シンボルを使用する必要がないように、ソース コード内の文字列の前にシンボルを付けることができます。

于 2012-05-28T02:41:10.690 に答える
2

使用する

name.Append(Path.Combine("NETWORK", "MyComputer");

文字列\にはエスケープ シーケンスがあります。したがって\、デバッガーでは\\

MSDN に準拠

Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.

Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ("). The following table lists the ANSI escape sequences and what they represent.

エスケープ シーケンスの読み取り

于 2012-05-28T02:39:14.883 に答える
0

あなたのコードはコンパイルできないと思います。\ はエスケープ文字であるため、文字列"\"は正しくありません。(リテラル)はそのエスケープを無視し、通常の文字として処理する@"\"ため、正しいです。@

詳細はこちら

于 2012-05-28T02:43:10.893 に答える