marcuthh
marcuthh

Reputation: 596

C# - Access to Path is Denied, unable to access files

I'm working on a project that a series of images that are deposited in one area of memory (say from a memory stick or download), and distributes them out to their respective folders based on the name of the image - which should correspond with the names of the files in their respective folder.

I have the first few functions of the code written to make this happen and decided to test it by altering the code so that it would carry out the process on a collection of the files in the My Pictures folder. What should have happened is that each file in the folder was copied to a folder in AppData called 'New Images', and added into the appropriate sub-directory or create the subdirectory if necessary.

This threw up an error stating that access to C:\Users\mark although it did not explain why or what to do about it. I thought this might be a problem with accessing the My Pictures folder so I copied the images into a folder called 'Test Images' inside the AppData folder (so the program would now be just transferring files between two folders in AppData). The same error occurred and I don't really know what to do next, I have written and read from files in AppData many times before and never encountered this problem. I have also read various entries related to this on this forum but don't seem to be able to get a definitive answer in terms of what to do next!

The code that causes the exception can be seen below:

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

Any help in where to look next would be greatly appreciated!

Thanks Mark

Upvotes: 0

Views: 1706

Answers (2)

m5c
m5c

Reputation: 567

Is this an ASP.net web application ? In that case can you try providing write permission to the IIS_IUSRS account.

The trick to solve this is ASP.net Impersonation .

Do not give full rights and run your app as an admin. Users will then have admin rights and I am sure you would want to avoid that.

Upvotes: 1

amit dayama
amit dayama

Reputation: 3326

For time being give full rights to everyone to your folder and try running your application as an administrator.

If that works then your can change permissions on your folder accordingly. Also make sure that your folder isn't readonly.

Upvotes: 1

Related Questions