vb.net - Issues with permissions when copying directories -
i need wit code, i've created visual basic program copies files , directories local drive network share, keep getting error saying access path c:\users\*username*\documents\my music
denied. though don't have sub-directory called my music
in documents
directory. appreciated. here code below:
imports system.io public class choices private sub choices_load(sender object, e eventargs) handles mybase.load end sub public sub btndocuments_click(sender object, e eventargs) handles btndocuments.click dim docsdirectory, destdocsdirectory, userdirectory, username, hdrive, mydocsdirectory, destmydocsdirectory string 'function pull user profile path hdrive = environment.getenvironmentvariable("homedrive") username = environment.getenvironmentvariable("username") userdirectory = environment.getenvironmentvariable("userprofile") docsdirectory = userdirectory + "\documents" destdocsdirectory = hdrive + username + "\my files" mydocsdirectory = "c:\my documents" destmydocsdirectory = hdrive + username + "\my documents" 'used error checking 'messagebox.show(sourcedirectory + vbcrlf + destdirectory) if (my.computer.filesystem.directoryexists(destdocsdirectory)) each foundfile string in my.computer.filesystem.getfiles(docsdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") select case lcase(path.getextension(foundfile)) case ".mks" case ".wav" case ".jpg" case ".wmv" case ".lnk" case ".png" case ".exe" case ".jpeg" case ".dll" case ".msi" case ".bmp" case ".url" case ".log" case ".dat" case ".ini" case ".propdesc" case ".arx" case ".hdi" case ".mc3" case ".css" case ".gif" case ".tif" case ".tiff" case ".htm" case ".chm" case ".pc3" case ".mp3" case ".mp4" case else my.computer.filesystem.copyfile(foundfile, destdocsdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end select next else my.computer.filesystem.createdirectory(destdocsdirectory) each foundfile string in my.computer.filesystem.getfiles(docsdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") select case lcase(path.getextension(foundfile)) case ".mks" case ".wav" case ".jpg" case ".wmv" case ".lnk" case ".png" case ".exe" case ".jpeg" case ".dll" case ".msi" case ".bmp" case ".url" case ".log" case ".dat" case ".ini" case ".propdesc" case ".arx" case ".hdi" case ".mc3" case ".css" case ".gif" case ".tif" case ".tiff" case ".htm" case ".chm" case ".pc3" case ".mp3" case ".mp4" case else my.computer.filesystem.copyfile(foundfile, destdocsdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end select next end if if (my.computer.filesystem.directoryexists(mydocsdirectory)) each foundfile string in my.computer.filesystem.getfiles(mydocsdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") select case lcase(path.getextension(foundfile)) case ".mks" case ".wav" case ".jpg" case ".wmv" case ".lnk" case ".png" case ".exe" case ".jpeg" case ".dll" case ".msi" case ".bmp" case ".url" case ".log" case ".dat" case ".ini" case ".propdesc" case ".arx" case ".hdi" case ".mc3" case ".css" case ".gif" case ".tif" case ".tiff" case ".htm" case ".chm" case ".pc3" case "." case else my.computer.filesystem.copyfile(foundfile, destmydocsdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end select next else messagebox.show(mydocsdirectory + "does not exist") end if end sub private sub btndesktop_click(sender object, e eventargs) handles btndesktop.click dim deskdirectory, destdeskdirectory, userdirectory, username, hdrive string hdrive = environment.getenvironmentvariable("homedrive") username = environment.getenvironmentvariable("username") userdirectory = environment.getenvironmentvariable("userprofile") deskdirectory = userdirectory + "\desktop" destdeskdirectory = hdrive + username + "\desktop" if (my.computer.filesystem.directoryexists(deskdirectory)) each foundfile string in my.computer.filesystem.getfiles(deskdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") select case lcase(path.getextension(foundfile)) case ".mks" case ".wav" case ".jpg" case ".wmv" case ".lnk" case ".png" case ".exe" case ".jpeg" case ".dll" case ".msi" case ".bmp" case ".url" case ".log" case ".dat" case ".ini" case ".propdesc" case ".arx" case ".hdi" case ".mc3" case ".css" case ".gif" case ".tif" case ".tiff" case ".htm" case ".chm" case ".pc3" case "." case else my.computer.filesystem.copydirectory(foundfile, destdeskdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end select next end if end sub end class
the directory called music
. magic windows displays my music
. try use physical name music
.
the analogous problematic exists my documents
, documents
.
if need documents directory of current user, can with
destmydocsdirectory = environment.getfolderpath(environment.specialfolder.mydocuments)
if want access directories of other users, need administrator, otherwise won't have rights access them!
you declared same lengthy select case
list several times. easier manage if put extensions in hashset(of string)
:
private m_mediaextensions new hashset(of string)() { ".mks", ".wav", ... }
then can test
if m_mediaextensions.contains(myextension) ... else ... end if
note: in vb collection initializers exist since vs2010. earlier versions can pass enumeration constructor:
m_mediaextensions = new hashset(of string)(new string() {".mks", ".wav", ...})
update in reponse comment. note: have not corrected paths yet.
imports system.io public class choices private m_mediaextensions hashset(of string) = _ new hashset(of string)(new string() {".mks", ".wav", ".jpg"}) public sub btndocuments_click(byval sender object, byval e eventargs) handles btndocuments.click dim docsdirectory, destdocsdirectory, userdirectory, username, hdrive, mydocsdirectory, destmydocsdirectory string 'function pull user profile path hdrive = environment.getenvironmentvariable("homedrive") username = environment.getenvironmentvariable("username") userdirectory = environment.getenvironmentvariable("userprofile") docsdirectory = userdirectory + "\documents" destdocsdirectory = hdrive + username + "\my files" mydocsdirectory = "c:\my documents" destmydocsdirectory = hdrive + username + "\my documents" 'used error checking 'messagebox.show(sourcedirectory + vbcrlf + destdirectory) if not directory.exists(destdocsdirectory) my.computer.filesystem.createdirectory(destdocsdirectory) end if each foundfile string in my.computer.filesystem.getfiles(docsdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") if not m_mediaextensions.contains(lcase(path.getextension(foundfile))) my.computer.filesystem.copyfile(foundfile, destdocsdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end if next if directory.exists(mydocsdirectory) each foundfile string in my.computer.filesystem.getfiles(mydocsdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") if not m_mediaextensions.contains(lcase(path.getextension(foundfile))) my.computer.filesystem.copyfile(foundfile, destmydocsdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end if next else messagebox.show(mydocsdirectory + "does not exist") end if end sub private sub btndesktop_click(byval sender object, byval e eventargs) handles btndesktop.click dim deskdirectory, destdeskdirectory, userdirectory, username, hdrive string hdrive = environment.getenvironmentvariable("homedrive") username = environment.getenvironmentvariable("username") userdirectory = environment.getenvironmentvariable("userprofile") deskdirectory = userdirectory + "\desktop" destdeskdirectory = hdrive + username + "\desktop" if directory.exists(deskdirectory) each foundfile string in my.computer.filesystem.getfiles(deskdirectory, _ fileio.searchoption.searchallsubdirectories, "*.*") if not m_mediaextensions.contains(lcase(path.getextension(foundfile))) my.computer.filesystem.copydirectory(foundfile, destdeskdirectory & "\" & path.getfilename(foundfile), showui:=fileio.uioption.alldialogs) end if next end if end sub end class
Comments
Post a Comment