View Single Post
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: compare files in c#

 
0
  #2
Jul 29th, 2008
The basics are there but a few suggestions for you:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6.  
  7. public class Program {
  8. private readonly string SERVER_PATH = @"\\server\directory";
  9. private readonly string LOCAL_PATH = @"C:\users\username\documents\images";
  10. private readonly string CONNECTION_STRING = "@some;sql=connection; string";
  11.  
  12. [STAThread]
  13. public static int Main( string[ ] args ) {
  14. List<string> filesInDatabase = GetDatabaseFileName();
  15. List<string> filesOnServer = GetServerFileNames();
  16.  
  17. foreach ( string fileName in filesOnServer ) {
  18. if ( !filesInDatabase.Contains(fileName) ) {
  19. // do copy code here which might look something like
  20. // the file names include the fullpath
  21. if ( File.Exists(fileName) ) {
  22. try {
  23. File.Copy(fileName, LOCAL_PATH + Path.GetFileName(fileName));
  24. } catch ( Exception exception ) {
  25. // Display some error notification here
  26. }
  27. }
  28. }
  29. }
  30. }
  31.  
  32. protected static List<string> GetDatabaseFileName( ) {
  33. List<string> returnValue = new List<string>();
  34.  
  35. using ( SqlConnection connection = new SqlConnection(CONNECTION_STRING) ) {
  36. connection.Open();
  37.  
  38. try {
  39. using ( SqlCommand command = connection.CreateCommand() ) {
  40. command.CommandType = CommandType.Text;
  41. command.CommandText = @"Place your SQL query here";
  42.  
  43. using ( SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection) ) {
  44. while ( dataReader.Read() ) {
  45. returnValue.Add(dataReader.GeString(0));
  46. }
  47. }
  48. }
  49. } catch ( Exception exception ) {
  50. throw exception;
  51. } finally {
  52. connection.Close();
  53. }
  54. }
  55.  
  56. return returnValue;
  57. }
  58.  
  59. protected static List<string> GetServerFileNames( ) {
  60. List<string> returnValue = new List<string>();
  61.  
  62. if ( Directory.Exists(SERVER_PATH) ) {
  63. string[ ] files = Directory.GetFiles(SERVER_PATH);
  64. returnValue.AddRange(files);
  65. }
  66.  
  67. return returnValue;
  68. }
  69. }

Now, I wrote that in notepad, so if there's a misspelling or a parse error don't complain. :-)
Reply With Quote