任何目录中的访问被拒绝。 WPF [英] Access denied in any directory. Wpf

查看:383
本文介绍了任何目录中的访问被拒绝。 WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的程序通过按p键来拍摄主窗口的快照。代码中没有带下划线的红色错误。



当程序运行时我按''p''它显示图像中的错误:



image - imgbb.com [ ^ ]



在希腊语中,它表示路径被拒绝访问.. 。



我在这里试过的路径:



Uri path = new Uri(@c:\\ \\screenshot.png);



它总是显示相同的错误。



任何想法可能导致这个或什么遗漏代码?



提前谢谢



我的尝试:



The program below is taking a snapshot of the main window by pressing the key ''p''. There are no underlined red errors in the code.

when the program is runing and I press ''p'' it shows the error in the image:

image — imgbb.com[^]

In greek it say access is denied to the path...

whatever path I tried here:

Uri path = new Uri(@"c:\screenshot.png");

it always show the same error.

Any ideas what may causes this or what is missing from the code?

Thanks in advance

What I have tried:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (linevertical.Visibility == Visibility.Hidden)
            { linevertical.Visibility = Visibility.Visible; }
            else { linevertical.Visibility = Visibility.Hidden; }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (lineo.Visibility == Visibility.Hidden)
            { lineo.Visibility = Visibility.Visible; }
            else { lineo.Visibility = Visibility.Hidden; }
        }

        private void Mainwindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.A)
            {
                lineo.Visibility = Visibility.Hidden;
                linevertical.Visibility = Visibility.Hidden;
            }
            if (e.Key == Key.S)
            {
                lineo.Visibility = Visibility.Visible;
                linevertical.Visibility = Visibility.Visible;

            }

            if (e.Key == Key.P)
            {
                //Set scrollviewer's Content property as UI element to capture full content
                UIElement element = mainwindow.Content as UIElement;
                Uri path = new Uri(@"c:\screenshot.png");
                CaptureScreen(element, path);
            }

        }
            public void CaptureScreen(UIElement source, Uri destination)
            {
                try
                {
                    double Height, renderHeight, Width, renderWidth;

                    Height = renderHeight = source.RenderSize.Height;
                    Width = renderWidth = source.RenderSize.Width;

                    //Specification for target bitmap like width/height pixel etc.
                    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
                    //creates Visual Brush of UIElement
                    VisualBrush visualBrush = new VisualBrush(source);

                    DrawingVisual drawingVisual = new DrawingVisual();
                    using (DrawingContext drawingContext = drawingVisual.RenderOpen())
                    {
                        //draws image of element
                        drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(0, 0), new Point(Width, Height)));
                    }
                    //renders image
                    renderTarget.Render(drawingVisual);

                    //PNG encoder for creating PNG file
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(renderTarget));
                    using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
                    {
                        encoder.Save(stream);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
    }
}

推荐答案

是的,在Windows Vista上,或者8或更高,C:\的根目录对于普通用户来说是ReadOnly。用户可以在C的根目录中创建文件夹,但不能创建文件。



您必须将截屏文件放在其他更合适的位置。



您从未说过您尝试过哪些其他路径,因此无法对这些路径发表评论。
Yeah, on Windows Vista, or maybe 8, and up, the root directory of C:\ is ReadOnly to normal users. Users can create folders in the root of C: but not files.

You're going to have to put your screenshot file somewhere else more appropriate.

You never said anything about which other paths you tried so it's impossible to comment on those.


您的启动驱动器的根目录受到保护,因此没有用户可以轻松更改 - 这是自Vista以来所有Windows操作系统的反病毒/恶意软件功能,不太可能改变。实际上,不建议对任何驱动器的根目录进行文件访问,并且您可以期望获得更多限制,而不是更少。



请参阅此处:我应该在哪里存储我的数据? [ ^ ]以获得更好的想法。
The root directory of your boot drive is protected, so no user can change it easily - this is an anti virus / malware feature of all Windows OSes since Vista and is unlikely to change. In fact, file access to the root of any drive is not recommended, and you can expect that to get more restrictive, not less.

See here: Where should I store my data?[^] for some better ideas.


这篇关于任何目录中的访问被拒绝。 WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆