Opening a file
-
Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:
private void button1_Click(object sender, EventArgs e)
{
string file_name = "C:/test1.txt";System.IO.StreamReader objReader; objReader = new System.IO.StreamReader(file\_name); textBox1.Text = objReader.ReadToEnd(); objReader.Close(); }
-
Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:
private void button1_Click(object sender, EventArgs e)
{
string file_name = "C:/test1.txt";System.IO.StreamReader objReader; objReader = new System.IO.StreamReader(file\_name); textBox1.Text = objReader.ReadToEnd(); objReader.Close(); }
Because it's easy to type! In this case the slash ("/") and backslash ("\") are both understood as the delimiter for path, so either one should work. However, backslash is used to escape in string literals, so to use backslash, you should escape it, like so: \\. You can see that is more typing than using just slash. If you have a lot of backslashes in your string literal, you can put @ in front of it so you don't need to escape them. For example,
string file_name = @"C:\test1.txt";
Still, there is the character @ you have to type. It's still more typing than simply using the forward slash.
-
Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:
private void button1_Click(object sender, EventArgs e)
{
string file_name = "C:/test1.txt";System.IO.StreamReader objReader; objReader = new System.IO.StreamReader(file\_name); textBox1.Text = objReader.ReadToEnd(); objReader.Close(); }
My guess is that it's simply a cheat to avoid having to type the requisite double backslash (special character):
string file_name = "C:\\test1.txt";
...Byte conservation; it's one byte shorter in the source file. ;P
-
Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:
private void button1_Click(object sender, EventArgs e)
{
string file_name = "C:/test1.txt";System.IO.StreamReader objReader; objReader = new System.IO.StreamReader(file\_name); textBox1.Text = objReader.ReadToEnd(); objReader.Close(); }
-
Because it's easy to type! In this case the slash ("/") and backslash ("\") are both understood as the delimiter for path, so either one should work. However, backslash is used to escape in string literals, so to use backslash, you should escape it, like so: \\. You can see that is more typing than using just slash. If you have a lot of backslashes in your string literal, you can put @ in front of it so you don't need to escape them. For example,
string file_name = @"C:\test1.txt";
Still, there is the character @ you have to type. It's still more typing than simply using the forward slash.
-
My guess is that it's simply a cheat to avoid having to type the requisite double backslash (special character):
string file_name = "C:\\test1.txt";
...Byte conservation; it's one byte shorter in the source file. ;P
-
Darrall wrote:
string file_name = "C:/test1.txt";
Because it works:). It transforms into "C:\\test1.txt"; But I don't. I use either
@"C:\test1.txt";//or
"C:\\test1.txt"; -
Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:
private void button1_Click(object sender, EventArgs e)
{
string file_name = "C:/test1.txt";System.IO.StreamReader objReader; objReader = new System.IO.StreamReader(file\_name); textBox1.Text = objReader.ReadToEnd(); objReader.Close(); }