FileStream 不允许我在本地计算机上创建文件?

发布于 2024-09-28 18:46:28 字数 533 浏览 0 评论 0原文

我正在使用 FileStream 将 FTP 服务器的信息下载到我的 C:\ 驱动器上的目录中。由于某种原因,即使我什至尝试将目录权限设置为“每个人”访问权限,它还是给了我这个异常:

System.UnauthorizedAccessException:对路径“C:\tmpfolder”的访问被拒绝”

这是为什么?这是我的代码的摘录。

byte[] fileData = request.DownloadData(dataMap.GetString("ftpPath") + "/" + content);
file = new FileStream(@"C:\tmpfolder", FileMode.Create, FileAccess.Write);
downloadedlocation = file.ToString();
file.Write(fileData, 0, fileData.Length);

另外,我的程序不在 ASP.NET 中,而只是一个 C# 控制台应用程序。

I'm using a FileStream to download information of an FTP server to a directory on my C:\ drive. For some reason, even though I've even tried setting the directory permissions to even 'Everyone' access, it's given me this exception:

System.UnauthorizedAccessException: Access to the path 'C:\tmpfolder' is denied'

Why is this? Here is an extract of my code.

byte[] fileData = request.DownloadData(dataMap.GetString("ftpPath") + "/" + content);
file = new FileStream(@"C:\tmpfolder", FileMode.Create, FileAccess.Write);
downloadedlocation = file.ToString();
file.Write(fileData, 0, fileData.Length);

Also, my program is not in ASP.NET and is just a C# console app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-10-05 18:46:28

如果存储文件的位置并不重要,请尝试

using System.IO;
.
.
.
string tempFile = Path.GetTempFileName();

这将在您的帐户临时文件夹中创建一个临时文件。不用担心权限问题;-)

If it doesn't matter where to store the file, try

using System.IO;
.
.
.
string tempFile = Path.GetTempFileName();

This will create a temporary file in your account temp folder. No concerns about permissions ;-)

嘿看小鸭子会跑 2024-10-05 18:46:28

我猜测您没有对 c:\ 尝试创建名为 tmpfolder 的文件的写入权限。

如果您的 c:\ 中存在名为 tmpfolder 的文件夹,请将代码更改为

file = new FileStream(@"C:\tmpfolder\myfile.tmp", FileMode.Create, FileAccess.Write);

hth

Mario

编辑:进一步说明:查看此链接 如何在 C# 中创建临时文件(用于写入)?,如果您在以下位置进行多个文件操作,则可能需要它同时。之后不要忘记删除文件。

I would guess that you do not have write privilege to c:\ where you try to create a file named tmpfolder.

If a folder named tmpfolder exists in your c:\ change your code to

file = new FileStream(@"C:\tmpfolder\myfile.tmp", FileMode.Create, FileAccess.Write);

hth

Mario

EDIT: On a further note: check out this link How to create a temporary file (for writing to) in C#? , you may need it if you have multiple file operations going on at the same time. Do no forget to delete the files afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文