public bool ProjectSetup(string project) {
string connectionString = string.Format("Provider=VFPOLEDB.1;Data Source={0};", project);
bool hasError = false;
string projectFileName = null;
List<string> fileList = null;
using (VfpQueryProvider provider = VfpQueryProvider.Create(connectionString, null)) {
provider.Connection.Open();
projectFileName = Path.GetFileName(project);
// Get a list of files with their relative path
fileList = (from item in provider.GetTable<VfpProjectItem>(projectFileName)
// exclude the header item
where item.Type != "H"
select item.Name).ToList();
provider.Connection.Close();
}
string homeDir = Path.GetDirectoryName(project);
this.Log.LogMessage("Default Directory: " + homeDir);
this.vfp.DoCmd(string.Format("cd [{0}]", homeDir));
// change the relative paths to absolute paths
for (int index = 0, total = fileList.Count; index < total; index++) {
string tempDir = homeDir;
string itemPath = fileList[index];
while (itemPath.StartsWith(@"..\")) {
// go up a directory level
tempDir = Path.GetDirectoryName(tempDir);
// remove ..\
itemPath = itemPath.Substring(3);
}
fileList[index] = Path.Combine(tempDir, itemPath);
}
// create a FileInfo object for each file
List<FileInfo> fileInfoList = fileList.Select(file => new FileInfo(file)).ToList();
// log an error message for each file that does not exist
fileInfoList.Where(fi => !fi.Exists).ToList().ForEach(fi => {
this.Log.LogError(string.Format("Cannot Locate File: {0}", fi.FullName));
hasError = true;
});
if (!hasError) {
string searchPath = string.Empty;
// create the search path
fileInfoList.Select(fi => fi.DirectoryName.ToUpper()).Distinct().ToList().ForEach(dir => {
searchPath += dir + ";";
});
if (!string.IsNullOrEmpty(searchPath)) {
this.Log.LogMessage("Search Path: " + searchPath);
this.vfp.DoCmd(string.Format("SET PATH TO [{0}]", searchPath));
}
}
if (!hasError && !string.IsNullOrEmpty(this.Debug) && !string.IsNullOrEmpty(this.VersionNumber)) {
string command = string.Format("MODIFY PROJECT '{0}'", project);
this.Log.LogMessage(command);
this.vfp.DoCmd(command);
#region Debug
if (!string.IsNullOrEmpty(this.Debug)) {
bool value = false;
if (bool.TryParse(this.Debug, out value)) {
command = string.Format("_vfp.ActiveProject.Debug = {0}", value ? ".t." : ".f.");
this.Log.LogMessage(command);
this.vfp.DoCmd(command);
}
else {
this.Log.LogError("Debug property is invalid: {0}", this.Debug);
hasError = true;
}
}
#endregion
#region VersionNumber
if (!string.IsNullOrEmpty(this.VersionNumber)) {
// make sure auto increment is set to false if a version number was specified
command = string.Format("_vfp.ActiveProject.AutoIncrement = .f.");
this.Log.LogMessage(command);
this.vfp.DoCmd(command);
command = string.Format("_vfp.ActiveProject.VersionNumber = '{0}'", this.VersionNumber);
this.Log.LogMessage(command);
this.vfp.DoCmd(command);
}
#endregion
}
return !hasError;
}