Jump to content

How do I find a file in C#?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Sp32

Sp32

    Newbie

  • Members
  • PipPip
  • 26 posts
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

#2
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
To search for files / folders, you first need to add the IO class...

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...");

            }

Posted Image

Posted Image

#3
Sp32

Sp32

    Newbie

  • Members
  • PipPip
  • 26 posts
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

#4
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

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

Yeah mate, thats the best thing to do to get the hang of it.

Also thanks mate, Merry christmas to you to :)

#5
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
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

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
Sp32

Sp32

    Newbie

  • Members
  • PipPip
  • 26 posts
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?

 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
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
replace search_pattern with "*.xml"
=) http://forum.codecal...86276&noquote=1

#8
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
so what you get with that is a string array

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