如果我using System.IO;
並運行以下命令:Fileinfo的對象
string myPathway = @"R:\Hello.pdf"
FileInfo x = new FileInfo(myPathway);
有沒有一種方法,所以我可以重用對象X,但在位於「mySecondPathway」不同的文件指出,或做我創建一個新的FileInfo對象y
?
如果我using System.IO;
並運行以下命令:Fileinfo的對象
string myPathway = @"R:\Hello.pdf"
FileInfo x = new FileInfo(myPathway);
有沒有一種方法,所以我可以重用對象X,但在位於「mySecondPathway」不同的文件指出,或做我創建一個新的FileInfo對象y
?
FileInfo x = new FileInfo("R:\\Hello.pdf"); // you forgot to escape the backslash
x = new FileInfo("anotherFile.txt"); // just reassign it
注意調動不重新使用內存中的實際對象,但x
剛纔指anotherFile.txt
文件。
(我編輯了字符串錯誤) – whytheq
無法重用FileInfo
-object,您需要創建一個新對象。不過,您可以重新分配x
與x = new FileInfo(mySecondPath);
。
不,你不能。您必須創建一個新對象,但可以重複使用x
,因爲x = new FileInfo(mySecondPath);
您可以重用_variable_,而不是_object_。如果你擔心內存使用,請不要。 –
+1(唯一讓我擔心的是,在我對這門語言充滿信心之前,需要多長時間......似乎很強大。) – whytheq