Finding Files

B

begum

HI everybody;

I have problem about selecting files in my main file. I have to find
the files whose ending INF. What Can I do?Can anybody help me?
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
public class FileClass
{
public static void Main(string[] args)
{
ReadFromFile("c:/a.txt");
}
static void ReadFromFile(string filename)
{
StreamReader SR;
string S;
SR = File.OpenText(filename);
S = SR.ReadLine();
while (S != null)
{
Console.WriteLine(S);
S = SR.ReadLine();
}
SR.Close();
}

}
}
 
J

Jon Skeet [C# MVP]

I have problem about selecting files in my main file. I have to find
the files whose ending INF. What Can I do?Can anybody help me?

It's not at all clear what you're trying to do. Do you want to call
ReadFromFile for every .inf file in a particular directory? Or does
a.txt contain filenames?

Look at Directory.GetFiles for an easy way of finding files on the
file system.

Jon
 
B

begum

It's not at all clear what you're trying to do. Do you want to call
ReadFromFile for every .inf file in a particular directory? Or does
a.txt contain filenames?

Look at Directory.GetFiles for an easy way of finding files on the
file system.

Jon



we have a main folder. and that folder has subfolders. we want to find
the .inf files in all subfolders.
 
J

Jon Skeet [C# MVP]

we have a main folder. and that folder has subfolders. we want to find
the .inf files in all subfolders.

In that case Directory.GetFiles is your friend - you can even ask it
to find recursively for you.

Jon
 
G

Guest

Yep, but only in .Net > 1.1, otherwise he would have to write his own
recursive method. (Or search for the 1000 examples in the net)
 
?

=?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?=

HI everybody;

I have problem about selecting files in my main file. I have to find
the files whose ending INF. What Can I do?Can anybody help me?
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
public class FileClass
{
public static void Main(string[] args)
{
ReadFromFile("c:/a.txt");
}
static void ReadFromFile(string filename)
{
StreamReader SR;
string S;
SR = File.OpenText(filename);
S = SR.ReadLine();
while (S != null)
{
Console.WriteLine(S);
S = SR.ReadLine();
}
SR.Close();
}

}

}
 
?

=?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?=

Yep, but only in .Net > 1.1, otherwise he would have to write his own
recursive method. (Or search for the 1000 examples in the net)

i think you refer to ones like this:

using System;
using System.Collection.Generic;
using System.IO;

public class Seeker
{

public static string[] RetrieveFilesFrom(string directory, string
searchPattern,int level)
{
List<string> filenames = new List<string>();
Stack<string> directories = new Stack<string>()

directories.Push(directory);

while(directories.count!=0)
{
string currentDirectory = directoriesStack.Pop();

foreach(string file in
Directory.GetFiles(currentDirectory,searchPattern))
filenames.Add(file);


if((level==-1) || (directories.count != level))
{
foreach(string subDirectory in
Directory.GetDirectories(currentDirectory))
directories.Push(subDirectory);
}

}
return filenames.ToArray();
}

}

public class Program
{

public static void Main(string[] args)
{
foreach(string path in RetrieveFilensFrom(@"C:\Program Files\",
"*.exe",-1)
Console.WriteLine(path);
Console.Read();
}

}

regards

horacio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top