|
-
Mar 23rd, 2012, 08:23 PM
#1
Thread Starter
Lively Member
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
-
Mar 24th, 2012, 06:54 AM
#2
Re: Code to open containing folder?
Moved From The CodeBank (which is for sharing code rather than posting questions )
-
Mar 24th, 2012, 07:58 AM
#3
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:
string filepath = "C:\\"; System.Diagnostics.Process prc = new System.Diagnostics.Process(); prc.StartInfo.FileName = filepath; prc.Start();
-
Mar 24th, 2012, 09:36 PM
#4
Re: Code to open containing folder?
 Originally Posted by kfcSmitty
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:
string filepath = "C:\\"; System.Diagnostics.Process prc = new System.Diagnostics.Process(); prc.StartInfo.FileName = filepath; 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:
string folderPath = @"C:\"; System.Diagnostics.Process.Start(folderPath);
-
Mar 25th, 2012, 10:00 AM
#5
Thread Starter
Lively Member
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.
-
Mar 25th, 2012, 05:16 PM
#6
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.
-
Mar 26th, 2012, 07:30 PM
#7
Thread Starter
Lively Member
Re: Code to open containing folder?
 Originally Posted by jmcilhinney
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
-
Mar 26th, 2012, 07:50 PM
#8
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.
-
Mar 26th, 2012, 07:58 PM
#9
Re: Code to open containing folder?
Your problem is this line:
C# Code:
string filepath = "e:\\serials";
You've hard-coded the e:\\serials, but you haven't appended your textbox.
What you need is
C# Code:
string filepath = string.Format("e:\\serials\\{0}",textBox1.Text); //OR string filepath = "e:\\serials\\" + textBox1.Text;
I put the 2nd one in, just incase it makes more sense to you.
-
Mar 26th, 2012, 07:59 PM
#10
Thread Starter
Lively Member
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.
-
Mar 26th, 2012, 08:05 PM
#11
Re: Code to open containing folder?
 Originally Posted by sentinelace
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?
-
Mar 26th, 2012, 08:11 PM
#12
Thread Starter
Lively Member
Re: Code to open containing folder?
 Originally Posted by jmcilhinney
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
-
Mar 26th, 2012, 08:29 PM
#13
Re: Code to open containing folder?
 Originally Posted by sentinelace
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|