Slavisha Slavisha - 3 months ago 37
C# Question

C# Passing the path from user input Box to a string

I am new in to C# programming and I have come across small issues i just cant wrap my mind around. I am creating small application which is going to have 3 input boxes as on screenshot below. When user click on Source button it will have freedom to chose the source folder. Once he select OK the Text Box next to the folder will be populated with path.So i got that part working.
Now the problem is that i am running out of the talent to figure out why that path from the box is not passed to the string in the code. For example, if i type the location of the List and put in string Item_List the code works fine. If i would to do same for the strings Source and Destination the app works perfectly fine. But when I try to have user set those variables by simply selecting the path destination it doesnt work. So just need to see what am i missing. Why the value from the text box which is C:\Files is not being passed to string Source = @"" . I am assuming I am handling this on wrong way due to "\" or it might be something else. Any ideas?

Appscreenshot

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

string Source = @"";
string Destination = @"";
string Item_List = @"C:\Users\Slavoljub Petkovic\Desktop\List.txt";


public Form1()
{
InitializeComponent();
}
//------------Form1 Load()----------------------------


private void Form1_Load(object sender, EventArgs e)
{

Source = tbSource.Text;
Destination = Convert.ToString(tbDestination.Text);


}
//-----------Move Files In The List-------------------

private void MoveFileInList()
{

StreamReader sr;
string curFile;
string to_file, from_file;
sr = File.OpenText(Item_List);
curFile = sr.ReadLine();
while (curFile != null)
{
to_file = Destination + "/" + curFile;
from_file = Source + "/" + curFile;
File.Move(from_file, to_file);
curFile = sr.ReadLine();
}
sr.Close();
}


private void label1_Click(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}


private void label3_Click(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
MoveFileInList();
}

private void label5_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void Box_Source_TextChanged(object sender, EventArgs e)
{

}

private void Box_List_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click_1(object sender, EventArgs e)
{


}

private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{

}

private void DestinationPath_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.tbDestination.Text = folderBrowserDialog1.SelectedPath;
}
}

private void SourcePath_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.tbSource.Text = folderBrowserDialog1.SelectedPath;
}
}

private void ListPath_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.tbList.Text = folderBrowserDialog1.SelectedPath;
}
}

private void Form1_Load_1(object sender, EventArgs e)
{

}

private void tbList_TextChanged(object sender, EventArgs e)
{

}
}
}

Answer

You are assigning the values of the Source and Destination textboxes to two variables in your form load event.

Source = tbSource.Text;
Destination = Convert.ToString(tbDestination.Text);

These variables than contain the text that is written in those textboxes at the moment the form loads and will not be updated when the text in the textboxes changes.

When you use those values in your MoveFileInList method, they will contain the values of the textboxes on load time and not the current value.

to_file = Destination + "/" + curFile;
from_file = Source + "/" + curFile;

What you want to do is read the current values of the textboxes.

to_file = Destination.Text + "/" + curFile;
from_file = Source.Text + "/" + curFile;