c# - File gets locked after setting attributes -


i have method saves object file. object gets modified , saved multiple times. problem when i'm trying save object second time same file, i'm getting unautorizedaccessexception. here code:

public void save(string path) {             string filename = string.format("{0}\\{1}", path, datafilename);             using (filestream fs = new filestream(filename, filemode.create))             {                 binaryformatter formatter = new binaryformatter();                 formatter.serialize(fs, this);                 file.setattributes(filename, fileattributes.hidden);             } } 

what's interesting, if comment line

file.setattributes(filename, fileattributes.hidden); 

problem disappears. how comes? , how can solve problem?

msdn says filemode.create:

specifies operating system should create new file. if file exists, overwritten. requires fileiopermissionaccess.write permission. filemode.create equivalent requesting if file not exist, use createnew; otherwise, use truncate. if file exists hidden file, unauthorizedaccessexception exception thrown.

which seeing. solution seems either use different mode, or suggested in comments, unhide -> save -> hide.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -