Results 1 to 13 of 13

Thread: Code to open containing folder?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    87

    Code to open containing folder?

    I have a ton of folders in a folder called "stuff". I have the folders inside this folder named by serial number. I want to be able to type in the serial number and browse the folder, if exists, then open the containing folder. Seems simple but not sure how to tackle it. Text box with a execute button

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to open containing folder?

    Moved From The CodeBank (which is for sharing code rather than posting questions )

  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code to open containing folder?

    Here is the code to open a folder in C#. To open a specific folder, you could just append the value to "filepath." Currently, this is hard-coded to open only c:\.

    C# Code:
    1. string filepath = "C:\\";
    2. System.Diagnostics.Process prc = new System.Diagnostics.Process();
    3. prc.StartInfo.FileName = filepath;
    4. prc.Start();

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Code to open containing folder?

    Quote Originally Posted by kfcSmitty View Post
    Here is the code to open a folder in C#. To open a specific folder, you could just append the value to "filepath." Currently, this is hard-coded to open only c:\.

    C# Code:
    1. string filepath = "C:\\";
    2. System.Diagnostics.Process prc = new System.Diagnostics.Process();
    3. prc.StartInfo.FileName = filepath;
    4. prc.Start();
    That's not incorrect but it's a little overdone. First, the path is for a folder and not a file, so the variable name should reflect that. Secondly, you can call Process.Start under such simple circumstances:
    C# Code:
    1. string folderPath = @"C:\";
    2.  
    3. System.Diagnostics.Process.Start(folderPath);

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    87

    Re: Code to open containing folder?

    When I type in the serial number it's just opening C:. It should open the folder on the c:\serialnumber

    Last edited by sentinelace; Mar 25th, 2012 at 10:14 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Code to open containing folder?

    If it's not working then you're doing it wrong. You haven't shown us what you're doing so we can't see what's wrong with it. Show us your exact code and the exact data you're using.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    87

    Re: Code to open containing folder?

    Quote Originally Posted by jmcilhinney View Post
    If it's not working then you're doing it wrong. You haven't shown us what you're doing so we can't see what's wrong with it. Show us your exact code and the exact data you're using.
    I am sure I am. I am still learning and a far away from being an expert. Here is my button

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string filepath = "e:\\serials";
                System.Diagnostics.Process prc = new System.Diagnostics.Process();
                prc.StartInfo.FileName = filepath;
                prc.Start();
            }
    
    
        }
    }
    When I hit the button, it just opens up e: but not the string that is entered in the txt box

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Code to open containing folder?

    Hmmm... not quite sure what the issue is there. I just tried this:
    Code:
    string filepath = "C:\\Windows";
    System.Diagnostics.Process prc = new System.Diagnostics.Process();
    prc.StartInfo.FileName = filepath;
    prc.Start();
    and the simpler (NB - imported System.Diagnostics):
    Code:
    Process.Start(@"C:\Windows");
    and both worked fine. If the path didn't exist or you didn't have access then I would expect an exception to be thrown, so I'm not sure what else it could be.

  9. #9
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code to open containing folder?

    Your problem is this line:

    C# Code:
    1. string filepath = "e:\\serials";

    You've hard-coded the e:\\serials, but you haven't appended your textbox.

    What you need is

    C# Code:
    1. string filepath = string.Format("e:\\serials\\{0}",textBox1.Text);
    2. //OR
    3. string filepath = "e:\\serials\\" + textBox1.Text;

    I put the 2nd one in, just incase it makes more sense to you.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    87

    Re: Code to open containing folder?

    That will open windows. How do I make it open the folder of the entered text? I want to enter text into the text box. This text box should look at c:\serials\serial#. If exist, open folder, if not, maybe a message.box that says "Folder not found". Maybe I wasn't clear?

    I changed to:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string filepath = string.Format("e:\\serials{0}",textBox1.Text);
                System.Diagnostics.Process prc = new System.Diagnostics.Process();
                prc.StartInfo.FileName = filepath;
                prc.Start();
            }
    
    
        }
    }
    but I get an error of "The system cannot find the file specified"
    Last edited by sentinelace; Mar 26th, 2012 at 08:08 PM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Code to open containing folder?

    Quote Originally Posted by sentinelace View Post
    That will open windows. How do I make it open the folder of the entered text? I want to enter text into the text box. This text box should look at c:\serials\serial#. If exist, open folder, if not, maybe a message.box that says "Folder not found". Maybe I wasn't clear?
    You're kidding, right? That is what's called "an example". It demonstrates that you can pass the path of a folder to Process.Start and it will open that folder. What the path is not relevant, as long as it is the path of a folder that exists and that you have access to.

    The path is simply a String, so you just have to create a String containing the path of the folder. So, to be specific, if the user enters "12345" into the TextBox, are you saying that you want to open a folder with the path "E:\12345"? "E:\serials\12345"? Something else?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    87

    Re: Code to open containing folder?

    Quote Originally Posted by jmcilhinney View Post
    You're kidding, right? That is what's called "an example". It demonstrates that you can pass the path of a folder to Process.Start and it will open that folder. What the path is not relevant, as long as it is the path of a folder that exists and that you have access to.

    The path is simply a String, so you just have to create a String containing the path of the folder. So, to be specific, if the user enters "12345" into the TextBox, are you saying that you want to open a folder with the path "E:\12345"? "E:\serials\12345"? Something else?
    Correct this is what I am looking for. I know it's an example. I am just saying in the previous example, not matter what I typed in the txt box, I would still get open folder of c:\windows

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Code to open containing folder?

    Quote Originally Posted by sentinelace View Post
    Correct this is what I am looking for. I know it's an example. I am just saying in the previous example, not matter what I typed in the txt box, I would still get open folder of c:\windows
    I'm not sure that do know it's an example because if you pass "C:\Windows" as the path then of course it's going to open "C:\Windows". You have to pass a String containing the path of the folder you want to open. If you want to open the folder specified by the value the user entered into a TextBox then you have to pass a String containing the value the user entered into a TextBox.

    If I gave you these two String:
    Code:
    string s1 = @"E:\";
    string s2 = "12345";
    how would you join them together? Like this, right?
    Code:
    string s3 = s1 + s2;
    The only difference in what you';re trying to do is that the second String is the Text of the TextBox. A String is still a String though, so you join them the same way. Once you've joined the drive path to the folder name then you have the full folder path, so you can pass it to Process.Start.

    That said, you should probably use Directory.Exists first to make sure that the folder exists.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width