المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : File.Move/create just won't work!



C# Programming
03-26-2013, 03:27 AM
Hello all, got myself a confusing mess here. I am going to provide as much information as possible, just let me know if something doesn't make sense.

On load, I have a file check and backup that is executed. The backup, doesn't work at all (the Move) and the create, only creates for the first item in the cluster. Here is the code.

//This pulls the current selected item in combo box to the string string instText = comboInst.GetItemText(comboInst.SelectedItem); //Items in the combobox have a leading 4 digit number so this //string simply grabs those 4 digits into a string string folder = instText.Substring(0, 4); //This is the path where each item's settings file is located string path = @"C:\Nightly\Institutions\" + folder + "\\"; //This names each email accordingly string email = folder + "Email.txt"; //This is the source path for the email file if it exists for //the backup process string sourceFile = @"C:\Nightly\Institutions\" + folder + "\\" + folder + "Email.txt"; //this is where the backups go to... it's the above path //with an archive directory string destinationFile = @"C:\Nightly\Institutions\" + folder + @"\Archive\"; //now I want to backup a file if it exists, if it doesn't create //the file... foreach (string file in settingsFiles) { if (File.Exists(path + email)) { File.Move(sourceFile, destinationFile); } if (!File.Exists(path + email)) { Directory.CreateDirectory(path); using (File.Create(path + email)) { /*Just creating a path*/ } }
What happens is, the file does NOT get moved if the file exists, and when it creates the file if a file doesn't exist, it only does it for the first item in the list, not each as you would expect a foreach statement to process.

If anyone could offer some insight on the wrongdoings of my ways I would appreciate it. Thanks!