访问路径在C#中被拒绝的错误 [英] Access to the path denied error in C#

查看:544
本文介绍了访问路径在C#中被拒绝的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一个类似的帖子,但我只是无法弄清楚这个问题。



我已经改变了Windows权限和改变路线。



当我试图挽救它抛出我异常的文件:访问路径****否认

 字符串路线=D:\\; 
的FileStream FS =新的FileStream(路线,FileMode.Create); < - 这里的问题是
的StreamWriter写=新的StreamWriter(FS);
耐心的人=新病人();
patient.name = textBox1.Text;
patient.name2 = textBox2.Text;


解决方案

您正在试图创建一个FileStream对象目录(夹)。指定一个文件名。(如:@D:\test.txt)和错误会消失。



顺便说一句,我会建议你使用StreamWriter的构造函数,一个编码作为其第二个参数,因为否则你可能会想稍后阅读保存的文件(使用的StreamReader)时不愉快的都会感到惊讶。


I have read a similar post, but i just cant figure out the problem.

I have changed the windows permissions and changed routes.

When i try to save a file it throws me the exception: Access to the path **** denied.

string route="D:\\";
FileStream fs = new FileStream(route, FileMode.Create); <--here is the problem
        StreamWriter write = new StreamWriter(fs);
        patient person = new patient();
        patient.name = textBox1.Text;
        patient.name2 = textBox2.Text;

解决方案

You are trying to create a FileStream object for a directory (folder). Specify a file name (e.g. @"D:\test.txt") and the error will go away.

By the way, I would suggest that you use the StreamWriter constructor that takes an Encoding as its second parameter, because otherwise you might be in for an unpleasant surprise when trying to read the saved file later (using StreamReader).

这篇关于访问路径在C#中被拒绝的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆