简体   繁体   中英

writing to disk: File access denied

I try to write my data to an Excel file. I'm using workbook method SaveAs. This is the code:

string FN = "C:\\Users\\Documents\\dane1.xlsx";
WB.SaveAs(FN, Excel.XlFileFormat.xlWorkbookNormal,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Excel.XlSaveAsAccessMode.xlShared,
            Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);

Everything is OK while the first parameter is hardcoded. When I try use a reference to object where I'm keeping path and file name (I've got it from SaveFileDialog) my application return me an exception:

File access denied".

What's wrong? Does anybody know how this can be avoided?

IF you're running Vista / Windows 7 you may need to run your application as Administrator to save to c:\\Users\\Documents (which generally doesn't exist)

Did you mean c:\\Users\\'username'\\Documents ?

in C# you could use Environment.GetEnvironmentVariable("userprofile") to get the users folder (ie c:\\Users\\Postman) - which is like type %userprofile% into run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM