using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public class Program {
private readonly string SERVER_PATH = @"\\server\directory";
private readonly string LOCAL_PATH = @"C:\users\username\documents\images";
private readonly string CONNECTION_STRING = "@some;sql=connection; string";
[STAThread]
public static int Main( string[ ] args ) {
List<string> filesInDatabase = GetDatabaseFileName();
List<string> filesOnServer = GetServerFileNames();
foreach ( string fileName in filesOnServer ) {
if ( !filesInDatabase.Contains(fileName) ) {
// do copy code here which might look something like
// the file names include the fullpath
if ( File.Exists(fileName) ) {
try {
File.Copy(fileName, LOCAL_PATH + Path.GetFileName(fileName));
} catch ( Exception exception ) {
// Display some error notification here
}
}
}
}
}
protected static List<string> GetDatabaseFileName( ) {
List<string> returnValue = new List<string>();
using ( SqlConnection connection = new SqlConnection(CONNECTION_STRING) ) {
connection.Open();
try {
using ( SqlCommand command = connection.CreateCommand() ) {
command.CommandType = CommandType.Text;
command.CommandText = @"Place your SQL query here";
using ( SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection) ) {
while ( dataReader.Read() ) {
returnValue.Add(dataReader.GeString(0));
}
}
}
} catch ( Exception exception ) {
throw exception;
} finally {
connection.Close();
}
}
return returnValue;
}
protected static List<string> GetServerFileNames( ) {
List<string> returnValue = new List<string>();
if ( Directory.Exists(SERVER_PATH) ) {
string[ ] files = Directory.GetFiles(SERVER_PATH);
returnValue.AddRange(files);
}
return returnValue;
}
}