0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Diagnostic_Tool_Blue_Screen
{

    public static class CreateDirectories
    {

        static string outputfiles;
        static string photofilesDir;
        static string photofilesDir1;
        static string photofilesDir2;
        static string photofilesDir3;
        static string temptxt;
        static string tempphotos;
        static string outputtext;
        static string outputphotos;

        public static void CreateDirectories()
        {

            outputfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OutputFiles");
            if (!Directory.Exists(outputfiles))
            {
                Directory.CreateDirectory(outputfiles);
            }
            else
            {
                Directory.Delete(outputfiles, true);
                Directory.CreateDirectory(outputfiles);
            }
            outputtext = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + outputfiles + "\\outputtext";
            if (!Directory.Exists(outputtext))
            {
                Directory.CreateDirectory(outputtext);
            }
            else
            {
                Directory.Delete(outputtext, true);
                Directory.CreateDirectory(outputtext);
            }
            outputphotos = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + outputfiles + "\\outputphotos";
            if (!Directory.Exists(outputphotos))
            {
                Directory.CreateDirectory(outputphotos);
            }
            else
            {
                Directory.Delete(outputphotos, true);
                Directory.CreateDirectory(outputphotos);
            }
            temptxt = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + outputfiles + "\\txtfiles";
            if (!Directory.Exists(temptxt))
            {
                Directory.CreateDirectory(temptxt);
            }
            else
            {
                Directory.Delete(temptxt, true);
                Directory.CreateDirectory(temptxt);
            }
            tempphotos = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\photosfiles";
            if (!Directory.Exists(tempphotos))
            {
                Directory.CreateDirectory(tempphotos);
            }
            else
            {
                Directory.Delete(tempphotos, true);
                Directory.CreateDirectory(tempphotos);
            }


            photofilesDir = outputphotos + "\\" + "photosfiles";
            if (!Directory.Exists(photofilesDir))
            {
                Directory.CreateDirectory(photofilesDir);
            }
            else
            {
                Directory.Delete(photofilesDir, true);
                Directory.CreateDirectory(photofilesDir);
            }
            photofilesDir1 = outputphotos + "\\" + "photosfiles1";
            if (!Directory.Exists(photofilesDir1))
            {
                Directory.CreateDirectory(photofilesDir1);
            }
            else
            {
                Directory.Delete(photofilesDir1, true);
                Directory.CreateDirectory(photofilesDir1);
            }
            photofilesDir2 = outputphotos + "\\" + "photosfiles2";
            if (!Directory.Exists(photofilesDir2))
            {
                Directory.CreateDirectory(photofilesDir2);
            }
            else
            {
                Directory.Delete(photofilesDir2, true);
                Directory.CreateDirectory(photofilesDir2);
            }
            photofilesDir3 = outputphotos + "\\" + "photosfiles3";
            if (!Directory.Exists(photofilesDir3))
            {
                Directory.CreateDirectory(photofilesDir3);
            }
            else
            {
                Directory.Delete(photofilesDir3, true);
                Directory.CreateDirectory(photofilesDir3);
            }
        }
    }
}

Im running my program from visual studio 2012 pro as admin. I ran the visual studio as admin.

For example now the exception error is on the line:

Directory.CreateDirectory(outputtext);

UnauthorizedAccessException Access to the path 'C:\Users\bout0_000\AppData\Local\OutputFiles\outputtext' is denied.

And also windows it self throw me a window in the middle of screen say: Location is not available and then inside i see description to check network to check if disk i inserted....etc.

  1. If im running my program as admin why i dont have access ?
  2. Is there any directories maybe i can use that have access to any users when its under admin rights ? Or maybe without admin rights ? Instead this:

C:\Users\bout0_000\AppData\Local\OutputFiles\outputtext

Maybe there is some other location at C:\Users\bout0_000\AppData\Local that is not need admin at all ? Maybe C:\Users\bout0_000\AppData\Local\Temp ?

This is the complete exception error:

System.UnauthorizedAccessException was unhandled
  HResult=-2147024891
  Message=Access to the path 'C:\Users\bout0_000\AppData\Local\OutputFiles\outputtext' is denied.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
       at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
       at Diagnostic_Tool_Blue_Screen.CreateDirectories.CreateDirectoriesAtConstructor() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\CreateDirectories.cs:line 40
       at Diagnostic_Tool_Blue_Screen.Form1..ctor() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Form1.cs:line 127
       at Diagnostic_Tool_Blue_Screen.Program.Main() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
DanielVest
  • 823
  • 4
  • 20
  • 39

1 Answers1

0

There are some things wrong in your code, give this modified version a try:

public static void CreateDirectories()
    {

        outputfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OutputFiles");
        if (Directory.Exists(outputfiles)) Directory.Delete(outputfiles, true);            
        outputtext = outputfiles + "\\outputtext";            
        Directory.CreateDirectory(outputtext);

        outputphotos = outputfiles + "\\outputphotos";
        Directory.CreateDirectory(outputphotos);

        temptxt = outputfiles + "\\txtfiles";
        Directory.CreateDirectory(temptxt);

        tempphotos = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\photosfiles";
        if (Directory.Exists(tempphotos)) Directory.Delete(tempphotos, true);
        Directory.CreateDirectory(tempphotos);            

        photofilesDir = outputphotos + "\\" + "photosfiles";            
        Directory.CreateDirectory(photofilesDir);

        photofilesDir1 = outputphotos + "\\" + "photosfiles1";
        Directory.CreateDirectory(photofilesDir1);

        photofilesDir2 = outputphotos + "\\" + "photosfiles2";
        Directory.CreateDirectory(photofilesDir2);

        photofilesDir3 = outputphotos + "\\" + "photosfiles3";
        Directory.CreateDirectory(photofilesDir3);
    }

Some notes:

  • It looks like that you want to delete the old outputfiles folder before creating a new one. Deleting with Directory.Delete(path,true) will delete all the subdirectories and files. So all the sub-folders (outputphotos, outputtext,...) obviously don't exist after you delete the outputfiles folder and create a new one. That means, we don't have to check if the sub-folders exist or not, just create new ones. The same logic is also applied to outputphotos folder.
  • You don't have to create parent directory first before creating the sub directory. Directory.CreateDirectory(path) will create all the parent directories needed before creating the sub-directory (the last in the path).
King King
  • 61,710
  • 16
  • 105
  • 130