gpt4 book ai didi

c# - 异常:使用 FileStream 时拒绝访问

转载 作者:行者123 更新时间:2023-11-30 13:59:51 25 4
gpt4 key购买 nike

以下行抛出异常。我不知道为什么。

using (var output = new FileStream(sftpFile.Name, FileMode.Create,FileAccess.ReadWrite))

异常(exception)是:

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,但没有解决。

最佳答案

UnauthorizedAccessException 错误消息告诉您您尝试打开的文件是什么:

C:\Users\roberth\Programming_Projects\Common\UI\bin\Debug

这看起来像目录名:您不能将目录作为文件打开。

您可能忘记附加文件名:

string filename = Path.Combine(sftpFile.Name, "SomeFile.dat");
using (var output = new FileStream(filename,...)
{
...
}

关于c# - 异常:使用 FileStream 时拒绝访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12532027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com