Reputation: 35557
If I'm using System.IO;
and have run the following:
string myPathway = @"R:\Hello.pdf"
FileInfo x = new FileInfo(myPathway);
Is there a method so I can reuse the object x but pointed at a different file located at "mySecondPathway", or do I create a new FileInfo object y
?
Upvotes: 0
Views: 441
Reputation: 155065
FileInfo x = new FileInfo("R:\\Hello.pdf"); // you forgot to escape the backslash
x = new FileInfo("anotherFile.txt"); // just reassign it
Note that reassignment doesn't re-use the actual object in-memory, but the x
just now refers to the anotherFile.txt
file.
Upvotes: 3
Reputation: 3686
No you cannot. You have to create a new object, but can reuse x
, as x = new FileInfo(mySecondPath);
Upvotes: 1
Reputation: 17522
There is no way to reuse a FileInfo
-object, you need to create a new one. You can just reassign x
with x = new FileInfo(mySecondPath);
though.
Upvotes: 2