I am around half way into a book called "Microsoft Visual C# 2010 Step-By-Step by John Sharpe" And now I want to try some coding..
I want to create a windows forms application what searches for a text file (I will add stuff like if true = messagebox.show("Found"); myself)
so I want to know the code what finds a file for windows forms.
I want the final build to be able to find a file within a certain folder/folders and then for it to send me that file to my email address
Thanks for reading
7 replies to this topic
#1
Posted 23 December 2010 - 11:05 PM
|
|
|
#2
Posted 24 December 2010 - 04:29 PM
To search for files / folders, you first need to add the IO class...
Once that has been added you can now add the main part of the code,
That code will search your computer for an existing file of your choice (using a textbox for the location)
The same applies for folders...

using System.IO;
Once that has been added you can now add the main part of the code,
if (File.Exists(textBox1.Text))
{
MessageBox.Show(textBox1.Text + " was found on your system...");
}
That code will search your computer for an existing file of your choice (using a textbox for the location)
The same applies for folders...
if (Directory.Exists(textBox1.Text))
{
MessageBox.Show(textBox1.Text + " was found on your system...");
}

#3
Posted 24 December 2010 - 10:16 PM
Thanks! You're a legend, I'm gonna have a play around with this for a while just to get some more practice.
Merry Christmas bud
Merry Christmas bud
#4
Posted 25 December 2010 - 08:42 PM
Sp32 said:
Thanks! You're a legend, I'm gonna have a play around with this for a while just to get some more practice.
Merry Christmas bud
Merry Christmas bud
Yeah mate, thats the best thing to do to get the hang of it.
Also thanks mate, Merry christmas to you to :)
#5
Posted 26 December 2010 - 07:35 AM
The only thing with that bit of code, is that it would only look for a specific location, which might not be what you're looking for.
example, if you were looking for a file named "sample.txt", but not sure where on the system, that wouldn't be much help.
you're probably looking for something more like this
where search pattern would allow you to specify the name of the file, supporting * wildcards, eg
*.txt
sample.txt
or just * for everything
be careful, you should be handing access violation typed exceptions, (in case you try enumerating files you don't have access to on the file system.)
also, probably don't do this at the root C:\, choose a directory like you My documents for playing with this.
example, if you were looking for a file named "sample.txt", but not sure where on the system, that wouldn't be much help.
you're probably looking for something more like this
System.IO.Directory.GetFiles(@"C:\", search_pattern, System.IO.SearchOption.AllDirectories);
where search pattern would allow you to specify the name of the file, supporting * wildcards, eg
*.txt
sample.txt
or just * for everything
be careful, you should be handing access violation typed exceptions, (in case you try enumerating files you don't have access to on the file system.)
also, probably don't do this at the root C:\, choose a directory like you My documents for playing with this.
#6
Posted 27 December 2010 - 01:25 AM
Thanks a lot sam_coder, I'll try your way as well
edit:
Lets say I am looking for a file but I don't know what it's called, all I know is that it is a single unique file type in a folder. For example it being something like *.xml in my downloads folder. Also if it's found to show a messagebox/return confirmation of file found, how would I implement that using your code?
I don't know where I'd put *.xml in your code
edit:
Lets say I am looking for a file but I don't know what it's called, all I know is that it is a single unique file type in a folder. For example it being something like *.xml in my downloads folder. Also if it's found to show a messagebox/return confirmation of file found, how would I implement that using your code?
System.IO.Directory.GetFiles(@"C:\Users\Sp32\Downloads", search_pattern, System.IO.SearchOption.AllDirectories);
I don't know where I'd put *.xml in your code
#7
Posted 27 December 2010 - 09:56 AM
#8
Posted 27 December 2010 - 10:00 AM
so what you get with that is a string array
you can process the results like this:
you can process the results like this:
foreach (string file_path in System.IO.Directory.GetFiles(@"C:\Users\Sp32\Downloads", "*.xml", System.IO.SearchOption.AllDirectories)) {
//do something with it, FileInfo is a nice tool at your disposal
FileInfo fi = new FileInfo(file_path);
//because file info exposes all kinds of usefull information, including file size, the name, parent path etc
//it also provides shortcuts to open the file for extracting or appending content.
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









