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

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -