0

I have 2 excel files: Employees.xlsx and Student.xlsx

Is it possible to switch the names of both files using C#?

Nathan
  • 13
  • 1
  • 6
  • Yes because I've already transferred the data that I need from the Employees to student already – Nathan Jan 16 '17 at 05:42
  • Use `System.IO.File.Move` to rename the files, using a temporary for the first, three rename operations in total. – Corey Jan 16 '17 at 06:06
  • @Corey Is there any examples that I could follow ? – Nathan Jan 16 '17 at 06:12
  • @Nathan It's the same problem as swapping the value of two variables, you need a third temporary to hold one of the values: `t = a; a = b; b = t;` Just do something similar with the filenames: `File.Move("Employees.xlsx", "temp.xlsx"); File.Move("Student.xlsx", "Employees.xlsx"); File.Move("temp.xlsx", "Student.xlsx");` – Corey Jan 16 '17 at 06:16
  • @Corey it didn't work though .. – Nathan Jan 16 '17 at 07:13
  • @Nathan Funny, works fine for me. "it didn't work" isn't descriptive enough. Why didn't it work? What went wrong? What error did you get? I'm going to guess that you've got one or both of the files open at the time, which is about the most common reason for move failure there is. Close the files and try again. – Corey Jan 17 '17 at 00:21
  • @Corey I've tried it again today and it works. Yeah I think yesterday it didnt work because my excel was still functioning when it wasn't even open. So I rewrote a few of the part of my codes and now it works perfectlyyy ! :D Thank you – Nathan Jan 17 '17 at 00:39

0 Answers0