Click here to Skip to main content
15,900,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I got an assignement to develop an application in C#.The application has the following functionality.
1>There will Be different folders ,Input files will be loaded at different time.
2>We have to check whether input file is present in a particular folder at particular time or not
Ex:Check the folder "X"every day at 5 PM,Check the folder "Y"every day at 6 PM,

I dont know how to start ..Could you please advice how to proceed this?
Posted

 
Share this answer
 
Comments
Prashant Bangaluru 17-Dec-12 21:31pm    
Thanks Suresh..The First link is really help ful for me..Author explained it very clear and good manner..I want to add one more question here how do I check for files in different folder at different time...?Can I use an xml whill will tell at what time which folder to check etc..Please advice me on this also
Take a look at the File Class[^] and Directory Class[^]
Here are some sample articles
Working With File Class in C#[^]
Working with Directory Class in .Net (C#)[^]
 
Share this answer
 
C#
FileInfo file = new FileInfo(yourFullPathOfFile);
if(file.Exists)
{
     //yes, file exists
}
else
{
    //no, file does not exists
}
 
Share this answer
 
1. Created a Sample XML file (XMLFile.xml)

XML
<?xml version="1.0" encoding="utf-8" ?>
<Information>
  <FolderInfo>
    <Folder>c:\\SampleFolder\Folder_X</Folder>
    <Time>12/18/2012 2:17:25 PM</Time>
    <Files>
      <file>XFile1.doc</file>
      <file>XFile2.txt</file>
      <file>XFile3.png</file>
      <file>XFile4.zip</file>
    </Files>
  </FolderInfo>
  <FolderInfo>
    <Folder>c:\\SampleFolder\Folder_Y</Folder>
    <Time>12/18/2012 2:19:25 PM</Time>
    <Files>
      <file>YFile1.doc</file>
      <file>YFile2.txt</file>
      <file>YFile3.png</file>
      <file>YFile4.zip</file>
    </Files>
  </FolderInfo>
  <FolderInfo>
    <Folder>c:\\SampleFolder\Folder_Z</Folder>
    <Time>12/18/2012 2:18:25 PM</Time>
    <Files>
      <file>ZFile1.doc</file>
      <file>ZFile2.txt</file>
      <file>ZFile3.png</file>
      <file>ZFile4.zip</file>
    </Files>
  </FolderInfo>
</Information>


2. After creating your windows service then add this method in the main class

private void CheckFileInFolder()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile.xml"));

XmlNode xnFolderInfo = xmlDoc.ChildNodes[1];

foreach (XmlNode xn in xnFolderInfo.ChildNodes)
{
string strFolder = xn.ChildNodes[0].InnerText;
DateTime dtTime = Convert.ToDateTime(xn.ChildNodes[1].InnerText);
if (DateTime.Now.ToString("dd/mm/yyyy hh:mm") == dtTime.ToString("dd/mm/yyyy hh:mm"))
{
foreach (XmlNode xnFiles in xn.ChildNodes[2])
{
string strFile = xnFiles.InnerText;
if (File.Exists(Path.Combine(strFolder, strFile)))
{
// Do Your Work here
}
}
}
}
}


Call this method on OnElapsedTime event
C#
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
  TraceService("Another entry at "+DateTime.Now);
  CheckFileInFolder()
}


Here DateTime.Now.ToString("dd/mm/yyyy hh:mm") == dtTime.ToString("dd/mm/yyyy hh:mm") is just a sample use your own condition here

you said that you need to Check the folder "X"every day at 5 PM,Check the folder "Y"every day at 6 PM

Take only the time in xml node instead of date and time.

XML
<FolderInfo>
   <Folder>c:\\SampleFolder\Folder_X</Folder>
   <Time>2:17:25 PM</Time>
   <Files>
     <file>XFile1.doc</file>
     <file>XFile2.txt</file>
     <file>XFile3.png</file>
     <file>XFile4.zip</file>
   </Files>
 </FolderInfo>
 <FolderInfo>
   <Folder>c:\\SampleFolder\Folder_Y</Folder>
   <Time>2:19:25 PM</Time>
   <Files>
     <file>YFile1.doc</file>
     <file>YFile2.txt</file>
     <file>YFile3.png</file>
     <file>YFile4.zip</file>
   </Files>
 </FolderInfo>


C#
DateTime dtTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + xn.ChildNodes[1].InnerText);
            if (DateTime.Now.ToString("hh:mm") == dtTime.ToString("hh:mm"))
            {
 
Share this answer
 
v4
Comments
Prashant Bangaluru 18-Dec-12 22:08pm    
Thanks Suresh..I feel that I can start this assignement today only..thanks for giving inputs at right time.
Suresh Dasari's 24-Dec-12 6:09am    
post your solution, after completion.
it will help others.
Prashant Bangaluru 9-Jan-13 4:37am    
Sorry Suresh.. The assignement is cancelled..But I ll do as a hobby assignement and post asap
Prashant Bangaluru 20-Feb-13 1:41am    
Hi Suresh, Now I am trying same logic with console application .We made it to run every 5 mins but the problem is we are loading the xml every 5 mins.Can you please tell me how to load an xml daily only once.
Suresh Dasari's 1-Mar-13 6:40am    
maintain the date into a static variable for first time, then check with that date every time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900