c# - Why am I getting this error sometimes : Location Is Not Available? -


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 program visual studio 2012 pro admin. ran visual studio admin.

for example exception error on line:

directory.createdirectory(outputtext); 

unauthorizedaccessexception access path 'c:\users\bout0_000\appdata\local\outputfiles\outputtext' denied.

and windows self throw me window in middle of screen say: location not available , inside see description check network check if disk inserted....etc.

  1. if im running program admin why dont have access ?
  2. is there directories maybe can use have access users when under admin rights ? or maybe without admin rights ? instead this:

c:\users\bout0_000\appdata\local\outputfiles\outputtext

maybe there other location @ c:\users\bout0_000\appdata\local not need admin @ ? maybe c:\users\bout0_000\appdata\local\temp ?

this complete exception error:

system.unauthorizedaccessexception unhandled   hresult=-2147024891   message=access path 'c:\users\bout0_000\appdata\local\outputfiles\outputtext' denied.   source=mscorlib   stacktrace:        @ system.io.__error.winioerror(int32 errorcode, string maybefullpath)        @ system.io.directory.internalcreatedirectory(string fullpath, string path, object dirsecurityobj, boolean checkhost)        @ system.io.directory.internalcreatedirectoryhelper(string path, boolean checkhost)        @ 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        @ 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        @ 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        @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args)        @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly()        @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx)        @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx)        @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state)        @ system.threading.threadhelper.threadstart()   innerexception:  

there things wrong in code, give modified version 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 want delete old outputfiles folder before creating new one. deleting directory.delete(path,true) delete subdirectories , files. sub-folders (outputphotos, outputtext,...) don't exist after delete outputfiles folder , create new one. means, don't have check if sub-folders exist or not, create new ones. same logic applied outputphotos folder.
  • you don't have create parent directory first before creating sub directory. directory.createdirectory(path) create parent directories needed before creating sub-directory (the last in path).

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -