Finding Files

  • Thread starter Thread starter begum
  • Start date Start date
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();
}

}
}
 
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
 
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.
 
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
 
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)
 
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();
}

}

}
 
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
 
Back
Top