简体   繁体   English

写入磁盘:文件访问被拒绝

[英]writing to disk: File access denied

I try to write my data to an Excel file. 我尝试将数据写入Excel文件。 I'm using workbook method SaveAs. 我正在使用工作簿方法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: 当我尝试使用对保留路径和文件名的对象的引用(我从SaveFileDialog获取它)时,我的应用程序向我返回异常:

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) 如果您正在运行Vista / Windows 7,则可能需要以管理员身份运行应用程序以保存到c:\\Users\\Documents (通常不存在)

Did you mean c:\\Users\\'username'\\Documents ? 您是说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. 在C#中,您可以使用Environment.GetEnvironmentVariable("userprofile")来获取用户文件夹(即c:\\ Users \\ Postman)-类似于运行%userprofile%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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