11 Replies - 372 Views - Last Post: 13 January 2013 - 12:00 PM Rate Topic: -----

#1 thetallestewokever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-January 13

Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 09:33 AM

Hi i am attempting to change the directory in a method in my program and i was wondering how i would then use the variable from the method to say that the directory has changed in a start screen method
 static void choice4()
        {
            Console.WriteLine("Please enter a new directory to go to: ");
            string newDirectory = Console.ReadLine();
            try
            {

                Directory.SetCurrentDirectory(newDirectory);

            }
            catch (DirectoryNotFoundException n)
            {

                Console.WriteLine("The Directory {0} does not exist", n);

            }
           
            Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(newDirectory));
            Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());

            Console.ReadLine();
            Console.Clear();

            startscreen();
        } 

any help would be much appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Changing the directory in a method and call variable elsewhere

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,306
  • Joined: 21-June 11

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 09:40 AM

View Postthetallestewokever, on 13 January 2013 - 05:33 PM, said:

i was wondering how i would then use the variable from the method to say that the directory has changed in a start screen method


I don't understand what you mean. Could you rephrase that?
Was This Post Helpful? 0
  • +
  • -

#3 thetallestewokever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-January 13

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 09:44 AM

I mean if i have a method that allows me to change the directory that is set at a main screen method as soon as the program loads how would i call the newDirectory variable in the start screen method ?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,306
  • Joined: 21-June 11

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 09:46 AM

newDirectory is a local variable, you can't access it from another method. But why would you need to? You changed the current directory - that change will affect any method that gets called afterwards, whether it has access to the newDirectory variable or not.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5629
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 09:48 AM

If its any help, I can't figure out what your goal is either, even after rephrasing it.

You can store as many directory variables as you like they are just strings.

string sourceDir = "C:\\source\\";
string destDir = "c:\\destination\\";


If you declare these at class level then you can use them in all your methods within that class.

Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing within a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod()
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}

This post has been edited by tlhIn`toq: 13 January 2013 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 thetallestewokever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-January 13

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 09:53 AM

i mean like this
static void startscreen()
        {

                Console.WriteLine("Where newDirectory should be")   
                Console.WriteLine();
                Console.WriteLine("Welcome to The Home Screen");
                Console.WriteLine();
                Console.WriteLine("Please Select One of The Following Options: ");
                
                string[] starting = new string [5] { "Full File Listing", "Filtered File Listing", "Folder Statistics", "Directory Switch", "Quit"};
                for (int count = 0; count < starting.Length; count++)
                {
                    Console.WriteLine ("{0}. {1}", count + 1, starting[count]);
                }
            
                int menuChoice;
                bool userNumber = int.TryParse(Console.ReadLine(), out menuChoice);
                while (userNumber == false || menuChoice < 1 || menuChoice > 5) //this says that if the user enters a number for the menu options that is less than 1 or higher than 4 it will send the message and the same options again
                {
                Console.Clear();
                Console.WriteLine("Input Incorrect , please type a number between 1 and 5");
                startscreen();
                userNumber = int.TryParse(Console.ReadLine(), out menuChoice);
                }
                
                if (menuChoice == 1)
                {
                    userNumber = true;
                    Program.choice1();
                }
                if (menuChoice == 2)
                {
                    Program.choice2();
                }
                if (menuChoice == 3)
                {
                    Program.choice3();
                }
                if (menuChoice == 4)
                {
                    Program.choice4();
                } 

i want where it has " where newDirectory should be" to display what the directory changed to in the setCurrentDirectory, if this helpos clear anything up at all
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,306
  • Joined: 21-June 11

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 10:12 AM

You can just call GetCurrentDirectory there. This will also work if the current directory hasn't been changed yet.

PS: You should use loops to manage the flow of your menu - not recursion. Your handling of invalid input for example will not work like you want.
Was This Post Helpful? 2
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5629
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 10:12 AM

Replace that quoted string with a variable or with the Directory.GetCurrentDirectory() method.

04 Console.WriteLine(Directory.GetCurrentDirectory())
Was This Post Helpful? 1
  • +
  • -

#9 thetallestewokever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-January 13

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 10:18 AM

Thankyou you two you're help was fantastic
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5629
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 10:45 AM

Lines 26-41... Get out of that bad habit of number if statements and read up on the switch construct.

switch (menuchoice)
{
    case 1:
       // do this
       break;
    case 2:
       // do this
    break;
}

Was This Post Helpful? 1
  • +
  • -

#11 thetallestewokever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-January 13

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 11:57 AM

I have a follow up question, i have qotten it to change the folder successfully but at the start of running the program it comes up with the folder as to where the project is saved, how can i set a directory at the start of the program and then change later in the directory change part ?
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5629
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Changing the directory in a method and call variable elsewhere

Posted 13 January 2013 - 12:00 PM

Easy Application Settings example
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1