【发布时间】:2012-09-13 23:09:06
【问题描述】:
以下行引发异常。我不知道为什么。
using (var output = new FileStream(sftpFile.Name, FileMode.Create,FileAccess.ReadWrite))
例外是:
Error: System.UnauthorizedAccessException: Access to the path 'C:\Users\roberth\
Programming_Projects\Common\UI\bin\Debug' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions
options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy,
Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at CWD.Networking.DownloadFromSftp(String hostname, String user, String passw
ord, Int32 port, String localPath, String remotePath, String filename) in c:\Use
rs\roberth\Programming_Projects\Common\Common\Common.cs:line 566
第 566 行是上面的 using 语句。
谁能解释我为什么会触发错误?我拥有该目录的完全权限,没有编译问题,我也可以在该目录中手动创建新文件和文件夹。
--编辑--
我尝试按照建议以管理员身份运行 VS,但没有解决。
【问题讨论】:
-
如果您使用 UAC(用户帐户控制)运行,请确保以管理员身份启动 Visual Studio。
-
@Arran 我们是,让我以管理员身份尝试
-
你能发帖
sftpFile.Name吗? (实际值,而不是您期望的值)是否已经存在具有该名称的文件(或目录)?例如,如果将文件名附加到路径失败,因此指向父目录,您会收到一些错误。 -
@CodesInChaos 我从 sftp 服务器下载,本地驱动器上不存在文件。如果该文件在本地确实存在,我希望它会引发不同的异常。为了获取文件,我正在运行
foreach(var sftpFile in client.ListDirectory(".")),如果我输出到控制台,它将正确显示文件的名称。
标签: c# exception administrator