How to set rootpath of application which is the path of a selected item in listbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user033088
    New Member
    • Mar 2013
    • 25

    #1

    How to set rootpath of application which is the path of a selected item in listbox?

    I want to set the rootpath of an executable application. This rootpath should be the same as the path of an item that I select from a ListBox. The code for selecting the item is as follows.
    Code:
    namespace Menu
    {
        public partial class Form1 : Form
        {
    
            private Button button;
            private ListBox listBox1;
            private string selecteditem;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                button = new Button();
                button.Text = "Create ListBox";
                button.AutoSize = true;
                button.Location = new Point(100, 150);
                button.Click += button1_Click;
                this.Controls.Add(button);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                listBox1 = new ListBox();
                listBox1.AutoSize = true;
                listBox1.Location = new Point(75, 100);
    
                listBox1.MultiColumn = true;
                listBox1.BeginUpdate();
                listBox1.DoubleClick += listBox1_DoubleClick;
                this.Controls.Add(listBox1);
    
    
                listBox1.Items.Add("N-TYPE1");
                listBox1.Items.Add("N-TYPE2");
                listBox1.Items.Add("N-TYPE3");
                listBox1.Items.Add("N-TYPE4");
                listBox1.Items.Add("N-TYPE5");
               
            }
    
            private void listBox1_DoubleClick(object sender, EventArgs e)
            {
                selecteditem = String.Empty;
    
                string path = "E:\\New Files\\";
                string subPath = "\\D1\\";
                string subPath2 = "\\S1";
    
                if (listBox1.SelectedItems.Count > 0)
                {
                    selecteditem = listBox1.SelectedItem.ToString();
                    if (!Directory.Exists(path + "\\" + selecteditem))
                    {
                        Directory.CreateDirectory(path + "\\" + selecteditem);
                    }
                    if (!Directory.Exists(path + "\\" + selecteditem + subPath))
                    {
                        Directory.CreateDirectory(path + "\\" + selecteditem + subPath);
                    }
                    if (!Directory.Exists(path + "\\" + selecteditem + subPath2))
                    {
                        Directory.CreateDirectory(path + "\\" + selecteditem + subPath2);
                    }
                    
                    else
                    {
                        //Do nothing
                    }
                    Console.WriteLine(path + selecteditem);
    
    
                    listBox1.ClearSelected();
                }
    
            }
        }
    }
    Now what I want is: Whatever I get the output as in the
    Console.Writeli ne(path + selecteditem), I would want to set this as a rootpath for an application so as to do rest of the measurements or steps only in tht path/folder.
    Is it possible to do something like this?
    Thank you in advance for your help.
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey user033088, After creating the directory copy and paste your application (with all the supporting files) which you want to start from directory. For this operation you can use 'File.Copy()' method. And to run that application you can use 'Process.Start( )' method.

    Code:
    System.IO.File.Copy("Source filename with path", "Destination filename with path");
    
    System.Diagnostics.Process.Start("Filename with path");

    Comment

    • user033088
      New Member
      • Mar 2013
      • 25

      #3
      I am sorry but could you please clear this for me. When you say "After creating the directory copy and paste your application (with all the supporting files) which you want to start from directory."
      Does this mean after I create the E:\\New Files\\ directory I should paste the application .exe file here? Because what I am looking for is basically changing the user control and am not sure how that is possible. Once the application starts the user basically has to browse through in order to set a path. I want to set this path as same as the selected item in listBox.
      Does this make sense? Many thanks is advance..!!

      Comment

      • vijay6
        New Member
        • Mar 2010
        • 158

        #4
        Hey user033088, i think i misunderstood your requirement. Can you explain your requirement briefly? What you want to save on that location?

        Comment

        • user033088
          New Member
          • Mar 2013
          • 25

          #5
          So with the code that I wrote till now, I get output as a path of whatever item is selected. For example if I Double click on N-TYPE4, firstly E:\New Files will be created and then E:\New Files\N-TYPE4. And, then the sub directories. What I am mainly interested is in the selected item path which is E:\New Files\N-TYPE4. Now, I have an application which basically is used for measurements of electrical components. This application once started has a option of selecting a rootpath on its menu/toolStrip (this is once the application is started or after you run it) I want this rootpath to be set as the one which we selected in the listBox which is E:\New Files\N-TYPE4.
          For now, in my application I am setting this path in my code as follows:
          Code:
           public VensTs()
                  {
                      Settings.Default.RootPath = "E:\\New Files";
                      Settings.Default.RootPathDatabase =
          "C:\\Users\\Public\\Documents\\Vens.Ts\\Database";
                      this.resetOnRootPathChange = true;
                      this.rootPath = Settings.Default.RootPath;
                      this.InitializeComponent();
                      this.Init();
                  }
          But I am looking for an easier way, if I select a Directory in the listBox, it should change this path as same as the listBox selected item. Does this make any sense? Thank You for replying.

          Comment

          • vijay6
            New Member
            • Mar 2010
            • 158

            #6
            Hey user033088, so you've two applications? One is for "measuremen ts of electrical components" and another one is to create directory?

            Comment

            • user033088
              New Member
              • Mar 2013
              • 25

              #7
              Yes both are different applications (Forms) I have another question posted asking for the same thing and you replied to that too. It is basically continuity of this part only. They are 2 different applications but I want the output (i.e the selected item path) to be used in form2.

              Comment

              • user033088
                New Member
                • Mar 2013
                • 25

                #8
                The first code here is used to create the directories. So basically it creates E:\New Files\N-TYPE4 and its sub directories after I double click on N-TYPE4. Now the second code is a part of code for the main application which is used for measurements of electrical components. Its a huge code so cannot post here but the main part where is I make change in the part where it is
                Code:
                Settings.Default.RootPath = "E:\\New Files";
                if I change here E:\\New Files\\N-TYPE4 then it is set but I want this to be done automatically. Like I do not want to make the change in the code every time. I want that whatever item is selected the application (measurement for electrical components) should know about it. Is it possible??
                Thank You for replying back to me!!

                Comment

                • vijay6
                  New Member
                  • Mar 2010
                  • 158

                  #9
                  Hey user033088, 'm again confused. In your another question you said "Passing the listBox1.Select edItem value from one form to another?" But here you said "Yes both are different applications".

                  Two different applications or one application with multiple forms?

                  If your requirement/question is 'one application with multiple forms' means try like this,

                  In 'Form1' while calling 'Form2' pass an argument as listbox1's selectedItem value. Like as follows,


                  Code:
                  Form2 form2 = new Form2(listbox1.SelectedItem.ToString());
                  form2.Show();


                  And in 'Form2' create a new constructor with one string argument. Like as follows,


                  Code:
                  public Form2(string value)
                  {
                          //MessageBox.Show(value);
                          //Settings.Default.RootPath = "E:\\New Files";
                          Settings.Default.RootPath = "E:\\New Files\\" + value;
                  }

                  Comment

                  Working...