My File.Copy(xxx,xxx, true) is throwing an unhandled exception I understand that File.copy does not allow me to use a directory but in my case my filename can change every time I go through the loop. I need the filename to be the same as it appears in my source folder. Here is what I have so far. Any Ideas? I looked at MSDN but it defines my problem, not a solution. Any help appreciated.
//Get Data from Filename string[] files = System.IO.Directory.GetFiles(sourcePath, "Result*.xml"); Regex date = new Regex(@"(?<month>[1-9]|[0-2])_(?<day>\d{2})_(?<year>\d{4})", RegexOptions.CultureInvariant); foreach (string s in files) { Match m = date.Match(s); if (m.Success) { //Pass Groups to String string month = m.Groups["month"].Value; string day = m.Groups["day"].Value; string year = m.Groups["year"].Value; //Create Dir var paths = new string[] { targetPath, year, month, day }; string result = paths.Aggregate(Path.Combine); Directory.CreateDirectory(result); //Copy file File.Copy(s, result, true); } }
File.Copyresult"...cannot be a directory" which is what you're passing it. Additionally it has examples that show using a path (I don't know where you got that it doesn't allow a path).