Reputation: 769
It keeps throwing an exception:
Ant JSON Serializer Failed: The process cannot access the file because it is being used by another process.
I know there are a lot of posts out there to fix that exception but it won't apply to my code. Can anyone point me to the right direction?
static string antJsonSerializer(){
#region KDI SALES
string[] allfiles = Directory.GetFiles(@"C:\xml\");
// Put all file names in root directory into array.
string sourceDirectory = @"C:\xml";
string destinationDirectory = @"C:\xml\Archive";
// Check if directories are existing
bool xmlRoot = System.IO.Directory.Exists(sourceDirectory);
if (!xmlRoot) System.IO.Directory.CreateDirectory(sourceDirectory);
bool xmlArchive = System.IO.Directory.Exists(sourceDirectory);
if (!xmlArchive) System.IO.Directory.CreateDirectory(sourceDirectory);
AntHelpers drone = new AntHelpers();
foreach (string name in allfiles)
{
try
{
drone.xmltosql(@name.Trim());
Directory.Move(sourceDirectory, destinationDirectory); //Archive
}
catch (Exception e)
{
//Console.WriteLine("Main Process Catch ERR: " + e.Message);
//ErrLogtoDB(string TRNTYPE, string extserial, string texttowrite, string logfilename)
AntHelpers.ErrLogtoDB("SALES", "", "Ant JSON Serializer Failed: " + e.Message,
"LeafCutterLogFile_JSONSerializer_" + (DateTime.Now.Year).ToString() + (DateTime.Now.Month).ToString().PadLeft(2, '0') + (DateTime.Now.Day).ToString().PadLeft(2, '0') + (DateTime.Now.Hour).ToString().PadLeft(2, '0') + ".html");
}
//drone.ExtractSQLSendAntHill(); //For testing: OFF
#endregion
}
return " !!!! Work Complete !!!! ";
}
Upvotes: 0
Views: 57
Reputation: 769
Thank you to both of you @C.Evenhuis & @manoj_rp. I fixed my File.Move(); It is now File.Move(@name, "ArchivedlogName.txt"); I'll up-vote both.
Upvotes: 0
Reputation: 169
Try this one:
// Ensure that the target does not exist.
if (File.Exists(destinationPath))
File.Delete(destinationPath);
// Move the file.
File.Move(sourcePath, destinationPath);
Refer File.Move Method for more details.
Upvotes: 1
Reputation: 26446
Your current code is trying to move the entire directory - try File.Move
instead:
string newPath = Path.Combine(destinationDirectory, Path.GetFileName(name));
File.Move(name, newPath);
Upvotes: 1