拒绝任何文件夹的C#访问

时间:2019-04-05 04:00:24

标签: c# windows winforms

当我在

中选择任何文件夹时
  

FolderDialogBrowser

我收到有关拒绝访问文件夹的错误。这适用于所有文件夹,文档,我的计算机,台式机等,实际上是每个文件夹。我了解到有关用户对文件夹(但磁盘上的每个文件夹都有用户访问权限)的访问权限,并且以管理员身份运行,但这无济于事。如果我将程序发送给朋友,他们也会使用文件夹访问权限来选择路径?我已经登录了管理员帐户,并且拥有所有权限,但是我的程序没有。

/*
 * Created by SharpDevelop.
 * User: Tomek
 * Date: 2019-04-05
 * Time: 04:26
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Linq;

namespace meta_generator
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }

        OpenFileDialog files = new OpenFileDialog();
        FolderBrowserDialog metaOutput = new FolderBrowserDialog();

        string metapath;

        void Button1Click(object sender, EventArgs e)
        {
            files.Filter = "Wszystkie pliki (*.*)|*.*";
            files.Multiselect = true;

            if (files.ShowDialog() == DialogResult.OK)
            {
                foreach (String file in files.FileNames)
                {
                    textBox1.Text = textBox1.Text + ";" + file;
                }
            }
        }

        void Button2Click(object sender, EventArgs e)
        {
            metaOutput.Description = "Wybierz folder gdzie zostanie wygenerowany plik meta.xml";
            metaOutput.RootFolder = Environment.SpecialFolder.MyDocuments;

            if (metaOutput.ShowDialog() == DialogResult.OK)
            {
                metapath = metaOutput.SelectedPath;
                textBox2.Text = metapath;
            }
        }
        void Button3Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0)
            {
                XDocument meta = new XDocument(new XElement("meta"));

                foreach (String file in files.FileNames)
                {
                    XElement childFileTag = new XElement("file");
                    XAttribute sourcepath = new XAttribute("src", file);
                    childFileTag.Add(sourcepath);

                    meta.Root.Add(childFileTag);
                }

                if (checkBox1.Checked)
                    meta.Root.Add(new XElement("oop", "true"));

                meta.Save(metapath);
            }
        }


    }
}

1 个答案:

答案 0 :(得分:3)

问题是您对

的使用
meta.Save(metapath);

metapath文件夹(目录)名称(例如c:\temp\,而不是文件名(例如c:\temp\bob.xml)。

保存文件时,需要保存到完整路径(包括文件名)。一个例子是:

meta.Save(Path.Combine(metapath, "bob.xml"));

或者,不要使用FolderBrowserDialog-而是使用SaveFileDialog来允许用户选择自己的文件名。

相关问题