FileStream 不允许我在本地计算机上创建文件?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果存储文件的位置并不重要,请尝试
这将在您的帐户临时文件夹中创建一个临时文件。不用担心权限问题;-)
If it doesn't matter where to store the file, try
This will create a temporary file in your account temp folder. No concerns about permissions ;-)
我猜测您没有对 c:\ 尝试创建名为 tmpfolder 的文件的写入权限。
如果您的 c:\ 中存在名为 tmpfolder 的文件夹,请将代码更改为
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
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.