簡體   English   中英

拒絕任何文件夾的C#訪問

[英]C# Access denied for any folder

當我在中選擇任何文件夾時

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


    }
}

問題是您對

meta.Save(metapath);

metapath文件夾 (目錄)名稱(例如c:\\temp\\ ,而不是文件名(例如c:\\temp\\bob.xml )。

保存文件時,需要保存到完整路徑(包括文件名)。 一個例子是:

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

或者,不要使用FolderBrowserDialog而是使用SaveFileDialog允許用戶選擇自己的文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM