-2

I have a textbox that generates a code based on the selections made by the user. I would like each possible code to correlate with copying and moving a folder into another. The folder is chosen by another textbox that allows the user to manually select the path for the new files to be moved to. What I am looking to do is set up a string of if/else if statements for each of the possible codes from textbox1. Take a look at my code below and see wha tyou find. Everything seems to work except for my statements uder

Private void button1_click...

        private void button1_Click(object sender, EventArgs e)
    {
        string destination = textBox1.Text;
        if (textBox2.Text == "111")
         String sourceFile = (@"C:\Program Files (x86)\OrganizerPro\TFSN  Internal Advisor to SWAG PPW"); 

        System.IO.File.Move(sourceFile, destination);
    }
Joe Pearson
  • 105
  • 10
  • your app most likely does not have access to "C:\Program Files ..." is that is what you are asking – Ňɏssa Pøngjǣrdenlarp Feb 28 '15 at 01:12
  • 1
    What is your question? – Christo S. Christov Feb 28 '15 at 01:15
  • A "string of if/else if statements" is very nearly never the right answer. Can you let us know the input codes and what the output should be? The code you have provided is almost irrelevant to your question. – Enigmativity Feb 28 '15 at 01:23
  • If you put braces after your if statement, you'll notice that `sourceFile` is declared inside them. This means that `sourceFile` *cannot* be used outside the braces. So it would error in your move, in fact, wouldnt compile. This is called the *scope* of your variables. – crthompson Mar 04 '15 at 15:36

2 Answers2

1

Your immediate problem might just be one of scoping, it looks like you try the move even if the if failed to set the value.

A list of If/else is not a very maintainable solution, you'll need to rebuild and redeploy each time the list of possibilities changes. Avoid this by reading the list from something that is external to the application.

However, what you describe is essentially a mapping between a code and a filename.

If your mapping really is static and you're happy for it to be baked into the application then you could define the mapping like this

        private Dictionary<string, string> mapping = new Dictionary<string, string>
    {
        { "111", @"c:\Program Files\File 1.txt" },
        { "112", @"c:\Program Files\File 2.txt" },
        { "113", @"c:\Program Files\File 3.txt" },
    };

You can retrieve values using some simple Linq

        var codeLookup = mapping.FirstOrDefault(kv => kv.Key == textBox2.Text);
        if (codeLookup.Key != null)
        {
            System.IO.File.Move(codeLookup.Value, destination);
        }
Andy Baker
  • 811
  • 1
  • 9
  • 19
  • How do I get entire directories though? essentially we have a bunch of folders that are used in certain situations and I want to have that folder and its contents copied and placed into a new folder that we have created which would be the value retrieved from textbox1. – Joe Pearson Feb 28 '15 at 03:03
  • I was thinking this: the string mapping retrieves values from textbox2 and sets the value of mapping which is the location of the documents we want to retrieve, Next we set destination which i simply want to retrieve from textbox1. – Joe Pearson Feb 28 '15 at 03:06
  • I took your advice and am getting this error when I run the code. what am i doing wrong? An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: Cannot create a file when that file already exists. – Joe Pearson Feb 28 '15 at 06:37
  • Take a look here. http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp – Andy Baker Feb 28 '15 at 08:20
  • @JoePearson [another case of using a dictionary](http://stackoverflow.com/a/28732139/2589202) instead of a bunch of if's. (+1) Please be sure to mark answers to your questions. – crthompson Mar 03 '15 at 19:57
0

Your question is not very clear. But there is something not logic in your code. this is a code behind method so change the method's access modifier from private to protected or public cause the event click does not reach this method

private void button1_Click(object sender, EventArgs e)

to

protected void button1_Click(object sender, EventArgs e)
Med.Amine.Touil
  • 1,225
  • 2
  • 11
  • 15