当前位置:编程学习 > C#/ASP.NET >>

C# 写文件问题

  string sPath = "f://Document//test";
               
         if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }

   FileStream myfs = new FileStream(sPath, FileMode.Create);
   StreamWriter sw = new StreamWriter(myfs);

    for (int i = 0; i < list.Count; i++)
                {
                    sw.WriteLine(list[i] + " ");

                }

                sw.Flush();
                sw.Close();

提示说对路径“f:\Document\test”的访问被拒绝。是怎么回事呢?谢谢~~ c# --------------------编程问答-------------------- 这个应该不是程序的问题,有可能你的系统装在F盘,然后禁止访问了,你设置下试试看 --------------------编程问答-------------------- 把这个文件的权限放到最大,试试看。 --------------------编程问答--------------------
右键,属性->如上图,开放读写权限 --------------------编程问答-------------------- 你往文件夹里写什么?你至少得建个文件吧。 --------------------编程问答-------------------- 可能是你的文件夹只有只读权限,设置一下权限吧 --------------------编程问答-------------------- 改下这行代码看看
FileStream myfs = new FileStream(sPath, FileMode.Create,FileAccess.ReadWrite); --------------------编程问答-------------------- 你用FileStream里面的参数是文件名,不是目录路径 --------------------编程问答-------------------- string sPath = "f://Document//test";//文件路径
string fPath = "f://Document";//文件夹             
         if (!Directory.Exists(fPath ))
                {
                    Directory.CreateDirectory(fPath );
                }

   FileStream myfs = new FileStream(sPath, FileMode.OpenOrCreate);
   StreamWriter sw = new StreamWriter(myfs);

    for (int i = 0; i < list.Count; i++)
                {
                    sw.WriteLine(list[i] + " ");

                }

                sw.Flush();
                sw.Close(); --------------------编程问答--------------------   string sPath = "f://Document//test";
这应该还有文件的后缀吧!
提示“拒绝访问”说明你的操作权限不够,最好把你要操作的文件放在非系统盘。 --------------------编程问答-------------------- string sPath = "f://Document//test"是路径,
Directory.CreateDirectory(sPath);这句代码创建了一个f://Document//test的文件夹

FileStream myfs = new FileStream(sPath, FileMode.Create);错误在这,sPath只是路径,并没有指定文件名,楼主如果是想在f://Document//test创建文件,还要指定文件名及后缀,可以改成这样

FileStream myfs = new FileStream(sPath+"//test.txt", FileMode.Create);//在sPath的目录下创建test.txt
--------------------编程问答-------------------- 问题解决了,真心各位大侠~~~ --------------------编程问答-------------------- 第一,sPath 到底是文件名还是路径名?你要写清楚,因为你前边有判断 if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }
所以,我认为你是路径名,那么,接下来你要把文件名给定义清楚。
--------------------编程问答-------------------- 第二,你用完myfs 之后,要把它Close掉。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,