【问题标题】:C# - Access to Path is Denied, unable to access filesC# - 访问路径被拒绝,无法访问文件
【发布时间】:2015-09-22 10:30:13
【问题描述】:

我正在开展一个项目,该项目将一系列图像存储在内存的一个区域(例如从记忆棒或下载)中,并根据图像的名称将它们分发到各自的文件夹中 - 这应与各自文件夹中的文件名相对应。

我编写了代码的前几个函数来实现这一点,并决定通过更改代码来测试它,以便它可以对“我的图片”文件夹中的文件集合执行该过程。 应该发生的是,文件夹中的每个文件都被复制到 AppData 中名为“新图像”的文件夹中,并添加到相应的子目录中,或者在必要时创建子目录。

这引发了一个错误,指出可以访问C:\Users\mark,尽管它没有解释为什么或如何处理它。 我认为这可能是访问“我的图片”文件夹时出现的问题,因此我将图像复制到 AppData 文件夹内名为“测试图像”的文件夹中(因此程序现在只是在 AppData 中的两个文件夹之间传输文件)。发生了同样的错误,我真的不知道下一步该怎么做,我之前已经多次写入和读取 AppData 中的文件,但从未遇到过这个问题。我还在这个论坛上阅读了与此相关的各种条目,但似乎无法就下一步做什么得到明确的答案!

导致异常的代码如下:

    //main folder (Contains sub-folders for each patient)
    string rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\New Images";
    //variables for sub-directories cannot be given at this point as they are created using a part of the image name
    string subDirectory;
    //string subDirectory = Path.Combine(rootDirectory, imageName.Split('_')[0]);
    string imageName;
    //string imageName = Path.GetFileName(image)
    string shortcutDirectory;
    //string shortcutDirectory = My Documents + Subfolder name + file name
    //list to hold all strings as bitmap image
    List<Bitmap> images = new List<Bitmap>();

    public void createDirectory()
    { 
        //create filing construct for all files passed in from machines

        //if main folder does not exist in AppData
        if (!Directory.Exists(rootDirectory))
        {
            //create it
            Directory.CreateDirectory(rootDirectory);
        }
    }

    public void saveLatestImages()
    {
        //specific path for My Pictures only
        string testImagesPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Test Images";
        //if there is a Pictures folder
        if (Directory.Exists(testImagesPath))
        {   
            //get number of files in folder
            int fileCount = Directory.GetFiles(testImagesPath).Count();

            //more than one file in folder
            if (fileCount > 0)
            {   
                //create data structures to store file info
                //filePaths holds path of each file represented as a string
                string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Test Images");

                //for each file in Pictures...
                for (int index = 0; index < fileCount; ++index)
                {
                    //get name of image at current index
                    imageName = filePaths[index];
                    //separate the part relating to the patient name (everything before (DD/MM/YYYY))
                    string subSpecifier = imageName.Split('_')[0];
                    //add to root directory to form subfolder name
                    subDirectory = Path.Combine(rootDirectory, subSpecifier);

                    //subdirectory name formulated, check for pre-existing
                    //subfolder does not exist
                    if(!Directory.Exists(subDirectory))
                    {
                        //create it
                        Directory.CreateDirectory(subDirectory); //ERROR OCCURS
                    }
                    //otherwise, file will be added to existing directory

                    //take everything from end and folder\file division to get unique filename
                    string fileName = imageName.Split('\\').Last();
                    //add this to the existing subDirectory
                    fileName = Path.Combine(subDirectory, fileName);

                    //copy the image into the subfolder using this unique filename
                    File.Copy(imageName, fileName);

                    //add full filename to list of bitmap images
                    images.Add(new Bitmap(fileName));

                    //update the shortcut to the file in the image storage shortcut folder
                    shortcutDirectory = getShortcut(subSpecifier, fileName);

                    //delete image at original path (clear folder so images not copied on next load up)
                    //File.Delete(imageName);
                }
            }
        }
    }

非常感谢您在下一步寻找的任何帮助!

谢谢 标记

【问题讨论】:

  • 您是否添加了 require 文件夹的权限,即 C:\Users\mark?
  • 我不知道,因为我说我是这个错误的新手,所以我什至不会考虑任何事前抵消它!我将如何添加权限? @AmitSoni

标签: c# access-denied


【解决方案1】:

这是一个 ASP.net Web 应用程序吗?在这种情况下,您可以尝试为 IIS_IUSRS 帐户提供写入权限。

解决这个问题的诀窍是 ASP.net Impersonation 。

不要授予全部权限并以管理员身份运行您的应用。然后用户将拥有管理员权限,我相信您会希望避免这种情况。

【讨论】:

  • 嗨,这听起来很棒而且很有帮助。正如我在问题中所述,我是这个错误的新手,并且根本不知道进行您建议的更改会涉及什么。你知道可以帮助我的链接或演练吗?
  • @marcuthh 。类似的讨论。也许这就是你要找的? stackoverflow.com/questions/14653722/…
  • forums.iis.net/t/… 也可能有帮助。
【解决方案2】:

暂时授予每个人对您文件夹的完全权限,并尝试以管理员身份运行您的应用程序。

如果可行,那么您可以相应地更改文件夹的权限。还要确保您的文件夹不是只读的。

【讨论】:

  • 我会在测试阶段尝试这个,谢谢您的意见!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2011-10-31
  • 2017-07-29
  • 2021-05-03
  • 2010-11-29
相关资源
最近更新 更多