【问题标题】:C# - Access negated to path - openFileDialogC# - 访问否定路径 - openFileDialog
【发布时间】:2014-07-01 20:45:26
【问题描述】:

我是开发新手,我制作了一个简单的应用程序来尝试打开文件应用程序返回错误:“访问路径被否定”但我是管理员,UAC 已禁用,我是唯一的帐户,为什么它给我这个错误?

这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {

                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        // Insert code to read the stream here.
                        string sourceCode = File.ReadAllText(Path.GetDirectoryName(openFileDialog1.FileName));
                        string colorizedSourceCode = new CodeColorizer().Colorize(sourceCode, Languages.Java);

                        textBox1.Text = colorizedSourceCode;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

【问题讨论】:

    标签: c# visual-studio openfiledialog access-denied


    【解决方案1】:

    在我看来,您正在尝试打开文件夹上的流。

    尝试替换它

    string sourceCode = File.ReadAllText(Path.GetDirectoryName(openFileDialog1.FileName))
    

    用这个:

    string sourceCode = File.ReadAllText(openFileDialog1.FileName)
    

    【讨论】:

      【解决方案2】:

      您打开流错误。

      也许可以试试这样?

      private void button1_Click(object sender, EventArgs e)
          {            
                  Stream myStream = null;
                  OpenFileDialog openFileDialog1 = new OpenFileDialog();
      
                  openFileDialog1.InitialDirectory = "c:\\";
                  openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                  openFileDialog1.FilterIndex = 2;
                  openFileDialog1.RestoreDirectory = true;
      
                  if (openFileDialog1.ShowDialog() == DialogResult.OK)
                  {
                      try
                      {
                          if ((myStream = openFileDialog1.OpenFile()) != null)
                          {
                              using (myStream)
                              {
                                  string strfilename = openFileDialog1.FileName;
                                  string filetext = File.ReadAllText(strfilename);
                                  textBox1.Text = filetext;
                                  button1.PerformClick();
                              }
                          }
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 2011-03-29
        • 1970-01-01
        • 2010-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多