using System;
using System.IO;
class Test
{    static void Main(string[] args)
    {
        string path = @"c:\MyTest.txt";
        try
        {
            if (!File.Exists(path))
            {
                StreamWriter sw = File.CreateText(path);
                sw.WriteLine("Hello");
                sw.Close();
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }        // Open the file to read from.
        StreamReader sr = null;
        try
        {
            sr = File.OpenText(path);
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        sr.Close();
    }
}
在XP的VS2008上运行是可以的,在WIN7的VS2008上运行不通过,求解释
异常是文件的访问被拒绝···

解决方案 »

  1.   

    NTFS是最麻烦的,哪怕是XP,换个用户连QQ都用不了
      

  2.   


    OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == DialogResult.OK)
                {                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                    //自己的操作
    }
      

  3.   

    试一试下面的步骤
    1. 以管理员身份登录
    2. 右键点击程序或者cmd(如果在cmd中运行的话)。 执行Run as Administrators
      

  4.   

    以管理员身份打开VS。
    或者将那个目的文件夹的Security里面,设置Everyone的Write权限。
      

  5.   


     OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == DialogResult.OK)
                {
    FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    //...
    }
    试下,这个XP NTFS可以打开文件
      

  6.   


    QQ不好,怎么怪NTFS,XP呢?微软的office你随便换用户不都是可以用的吗?
    QQ为什么不让你换用户?还不是有私货,就想着悄悄升级,悄悄下载,怕你权限给他挡了,恨不得你只用administrator运行。