简体   繁体   English

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

[英]C# Access denied for any folder

When i selected any folder in 当我在中选择任何文件夹时

FolderDialogBrowser FolderDialogBrowser

i got error about access denied for folder. 我收到有关拒绝访问文件夹的错误。 That's for everything folders, documents, my computer, desktop etc, really every folder. 这适用于所有文件夹,文档,我的计算机,台式机等,实际上是每个文件夹。 I was read about user access for folders (but every folder on disk?), and run as administrator, but it don't help me. 我了解到有关用户对文件夹(但磁盘上的每个文件夹都有用户访问权限)的访问权限,并且以管理员身份运行,但这无济于事。 If i send me program to friend, they will have too play with folders access to select path? 如果我将程序发送给朋友,他们也会使用文件夹访问权限来选择路径? I'm logged on admin account and i have all permissions, but my program no. 我已经登录了管理员帐户,并且拥有所有权限,但是我的程序没有。

/*
 * 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);
            }
        }


    }
}

The issue is your use of 问题是您对

meta.Save(metapath);

metapath is a folder (directory) name (like c:\\temp\\ , not a filename (like c:\\temp\\bob.xml ). metapath文件夹 (目录)名称(例如c:\\temp\\ ,而不是文件名(例如c:\\temp\\bob.xml )。

When saving a file, you need to save to a complete path (including filename). 保存文件时,需要保存到完整路径(包括文件名)。 An example would be: 一个例子是:

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

Alternatively, don't use FolderBrowserDialog - instead use SaveFileDialog to allow the user to choose their own filename. 或者,不要使用FolderBrowserDialog而是使用SaveFileDialog允许用户选择自己的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM