Hi guys,

I have a program im writing that needs to look for a file in a certain directory then when it find the file add the contents to a database.

I have the program working which does this however the filename changes depending on when it was created. The filename structure is as follows:

info_(datetime)

Where datetime changes depending on when it was created. So i basically need some code to look for the first file in a directory that start with info_ and it into a variable and then for that file execute a set of commands (i have this code already) then delete the file from the directory and move onto the next file. However new files will be dropped into the directory all the time so it needs to be in a loop. Any help wld be much apreciated

Recommended Answers

All 3 Replies

Take a look at FileSystemWatcher (a component in the tool box).
It will detect when new files have been added to a directory.

Otherwise, you can use something like this is a timed loop:

DirectoryInfo dirinfo = new DirectoryInfo("C:\\temp");
   foreach (FileInfo fi in dirinfo.GetFiles("info_*"))
   {
         // do something with file
         fi.Delete();
   }

cool thats very useful,

trying the example you give i then need to open the file and read the lines:

StreamReader objReader = new StreamReader(fi);

But it gives errors:

Error	1	The best overloaded method match for 'System.IO.StreamReader.StreamReader(string)' has some invalid arguments	C:\Users\chris\Documents\work\uni\year3\project\visualproject\ConsoleApplication1\ConsoleApplication1\reports.cs	41	46	ConsoleApplication1
Error	2	Argument '1': cannot convert from 'System.IO.FileInfo' to 'string'	C:\Users\chris\Documents\work\uni\year3\project\visualproject\ConsoleApplication1\ConsoleApplication1\reports.cs	41	63	ConsoleApplication1

I think streamreader is expecting a string what can i do to correct this?

fi.FullName

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.