Reputation: 91
Everything works perfectly upto when I try to create the key, I can't seem to interpolate the drive label.
string BID = Sys.GetBaseboardID();
string CID = Sys.GetMachineGuID();
string k = "32 char string"; // this is where my 256 bit key goes
string iv = "16 char string"; // this is where my 128 bit key goes
string bencr = AES.Encrypt(BID, k, iv); //sorry for my strange variable names
string cencr = AES.Encrypt(CID, k, iv);
string keydir = "{dName}:\b4nk.key"; // <-- Here is the problem
StreamWriter w_r;
w_r = File.CreateText(@keydir);
w_r.WriteLine("WARNING: TOUCHING THIS FILE WILL REMOVE YOUR AUTHENTICATION\n");
w_r.WriteLine(bencr);
w_r.WriteLine(cencr);
w_r.Close();
File.SetAttributes(@keydir, FileAttributes.Hidden | FileAttributes.ReadOnly);
As you can most likely imagine, the output of the keydir
string is the following: {dName}:4nk.key
There are a couple problems:
@
character at the start.string keydir = dName + ":\b4nk.key"
although it works, the \b problem remains.thanks to @Joshua Robinson I have learned something really interesting, my recent usage of python had me confused here.
to fix this you can combine the $
and @
characters to get an interpolated, verbatim string literal:
string keydir = $@"{dName}:\b4nk.key";
Upvotes: 2
Views: 1337
Reputation: 37113
You´re mixing two things here: string-interpolation and a verbatim string.
The first one is indicated using the $
infront:
var myString = $"{ data }whateverComesNext";
The verbatim simply escapes any slahes. As you already assumed \b
is an escape-sequence. You have to escape that sequence, e.g. by using \\b
. Alternativly use both verbatim and interpolation:
var myString = $@"{ data }\banyothertext"`.
Upvotes: 2
Reputation: 3547
Check out the documentation on string interpolation.
The $ special character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results. This feature is available starting with C# 6.
So, in order to get an interpolated string, your string literal needs to begin with the $
character.
string keydir = $"{dName}:\b4nk.key";
You should also check out the documentation on verbatim strings. Specifically, the second item in the list which describes verbatim string literals.
To indicate that a string literal is to be interpreted verbatim. The @ character in this instance defines a verbatim string literal. Simple escape sequences (such as "\" for a backslash), hexadecimal escape sequences (such as "\x0041" for an uppercase A), and Unicode escape sequences (such as "\u0041" for an uppercase A) are interpreted literally. Only a quote escape sequence ("") is not interpreted literally; it produces one double quotation mark. Additionally, in case of a verbatim interpolated string brace escape sequences ({{ and }}) are not interpreted literally; they produce single brace characters. The following example defines two identical file paths, one by using a regular string literal and the other by using a verbatim string literal. This is one of the more common uses of verbatim string literals.
It appears you've added the special @
character to the name of your variable, keydir
. Using the @
character at the beginning of a variable name would allow you to use a keyword as a variable name, for example if you wanted a variable named class
you could name it @class
. Placing @
at the start of the variable name will not cause it to be interpreted literally if it is a string
, you have to place it at the front of the string
literal.
You can combine the $
and @
characters to get an interpolated, verbatim string literal:
string keydir = $@"{dName}:\b4nk.key";
Upvotes: 2
Reputation: 127
If you mean to have in file {dName}:\b4nk.key
not {dName}:4nk.key
you can replace all '\' chars to '\\'. \ breaks another \ and it remains \ but without it's function or simply add @ before string
string a = "asdak\\nsa\\b"
Or string a = @"asdak\nsa\b";
I hope it helps
Upvotes: -1