Ordner Kopieren Verweigert

  • VB.NET

Es gibt 31 Antworten in diesem Thema. Der letzte Beitrag () ist von Keybladmaster.

    Ordner Kopieren Verweigert

    Hey, Hey

    Ich Versuche einen Ordner mit Inhalt zu Kopieren, mein Programm besitzt Admin Rechte.
    Sobald ich das Versuche bekomme ich den Fehler das der Zugriff Verweigert wurde.

    VB.NET-Quellcode

    1. try
    2. My.Computer.FileSystem.CopyDirectory(ordner, My.Application.Info.DirectoryPath & "/bin", True)
    3. Catch ex As Exception
    4. MessageBox.Show(ErrorToString.ToString())
    5. end try
    6. 'Habe es auch so Versucht aber auch selbes Resultat
    7. Try
    8. Dim C As String = Environment.ExpandEnvironmentVariables("%LocalAppData%")
    9. Shell("xcopy " & C & " " & My.Application.Info.DirectoryPath & "/bin/backup/section1" & " /E /C /Q /I /Y")
    10. Catch ex As Exception
    11. MessageBox.Show(ErrorToString.ToString)
    12. End Try


    Kann mir da einer Helfen?

    LÖSUNG :

    Der Ordner kann nicht Kopiert werden!
    Aber

    Man kann ihn Zippen und dann Entpacken.
    Ich habe dazu diese DLL benutzt :
    icsharpcode.net/opensource/sharpziplib/Download.aspx

    Code :
    Spoiler anzeigen


    Ein Verweis auf die DLL muss da sein und die Klasse auch!

    VB.NET-Quellcode

    1. 'Erstellen eines Ordners und des Pfades
    2. System.IO.Directory.CreateDirectory("Dein Pfad")
    3. Dim directorypath as String = Environment.SpecialFolder.LocalApplicationData
    4. 'Zwar nicht überall gesehen aber hier ein muss (für die Dateien die Verwendet werden oder Kopier Geschütz sind)
    5. On Error Resume Next
    6. 'ZipLib Initialisieren und Zip erstellen
    7. Dim ZipLib As New ZipLib
    8. ZipLib.CompressionLevel = 6 'Kein Muss
    9. ZipLib.CompressDirectory(ordner, "namederZip.zip", "/ordner_wo_es_gespeichert_werden_soll") 'das .zip muss hinten dran!!
    10. 'Entpacken
    11. ZipLib.DecompressFile("File.zip","/ordner_wo_es_gespeichert_werden_soll")


    Code zur ZipLib.vb (Klasse)

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.Collections
    3. Imports System.Collections.Generic
    4. Imports System.Data
    5. Public Class ZipLib
    6. Private mCompressionLevel As Integer = 6
    7. Public Property CompressionLevel() As Integer
    8. Get
    9. Return mCompressionLevel
    10. End Get
    11. Set(value As Integer)
    12. mCompressionLevel = value
    13. End Set
    14. End Property
    15. Public Sub CompressDirectory(InputDir As String, FileName As String, OutputDir As String)
    16. Dim Files As New List(Of String)()
    17. Dim RelativePath As String = Nothing
    18. GetAllFiles(InputDir, Files)
    19. If String.IsNullOrEmpty(OutputDir) Then
    20. OutputDir = Path.GetDirectoryName(InputDir)
    21. End If
    22. If Directory.Exists(OutputDir) = False Then
    23. Directory.CreateDirectory(OutputDir)
    24. End If
    25. Dim ZFS As New FileStream(OutputDir & "" & FileName, FileMode.Create)
    26. Dim ZOut As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ZFS)
    27. ZOut.SetLevel(6)
    28. Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
    29. Dim Buffer As Byte() = New Byte(4096) {}
    30. Dim ByteLen As Integer = 0
    31. Dim FS As FileStream = Nothing
    32. Dim ParentDirLen As Integer = InputDir.Length + 1
    33. For i As Integer = 0 To Files.Count - 1
    34. RelativePath = Files(i).Substring(ParentDirLen)
    35. ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(RelativePath)
    36. ZipEntry.DateTime = System.DateTime.Now
    37. ZOut.PutNextEntry(ZipEntry)
    38. FS = New FileStream(Files(i), FileMode.Open, FileAccess.Read, FileShare.Read)
    39. Do
    40. ByteLen = FS.Read(Buffer, 0, Buffer.Length)
    41. ZOut.Write(Buffer, 0, ByteLen)
    42. Loop While Not (ByteLen <= 0)
    43. FS.Close()
    44. Next
    45. ZOut.Finish()
    46. ZOut.Close()
    47. ZFS.Close()
    48. End Sub
    49. Public Sub CompressDirectory(InputFiles As List(Of String), FileName As String, OutputDir As String)
    50. Dim RelativePath As String = Nothing
    51. If Directory.Exists(OutputDir) = False Then
    52. Directory.CreateDirectory(OutputDir)
    53. End If
    54. Dim ZFS As New FileStream(OutputDir & "" & FileName, FileMode.Create)
    55. Dim ZOut As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ZFS)
    56. ZOut.SetLevel(6)
    57. Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
    58. Dim Buffer As Byte() = New Byte(4096) {}
    59. Dim ByteLen As Integer = 0
    60. Dim FS As FileStream = Nothing
    61. Dim ParentDirLen As Integer = Path.GetDirectoryName(InputFiles(0)).Length
    62. For i As Integer = 0 To InputFiles.Count - 1
    63. RelativePath = InputFiles(i).Substring(ParentDirLen)
    64. ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(RelativePath)
    65. ZipEntry.DateTime = System.DateTime.Now
    66. ZOut.PutNextEntry(ZipEntry)
    67. FS = New FileStream(InputFiles(i), FileMode.Open, FileAccess.Read, FileShare.Read)
    68. Do
    69. ByteLen = FS.Read(Buffer, 0, Buffer.Length)
    70. ZOut.Write(Buffer, 0, ByteLen)
    71. Loop While Not (ByteLen <= 0)
    72. FS.Close()
    73. Next
    74. ZOut.Finish()
    75. ZOut.Close()
    76. ZFS.Close()
    77. End Sub
    78. Public Sub CompressFiles(InputFiles As List(Of String), FileName As String, OutputDir As String)
    79. Dim ZFS As New FileStream(OutputDir & "" & FileName, FileMode.Create)
    80. Dim ZOut As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ZFS)
    81. ZOut.SetLevel(6)
    82. Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
    83. Dim Buffer As Byte() = New Byte(4096) {}
    84. Dim ByteLen As Integer = 0
    85. Dim FS As FileStream = Nothing
    86. For i As Integer = 0 To InputFiles.Count - 1
    87. ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(InputFiles(i)))
    88. ZipEntry.DateTime = System.DateTime.Now
    89. ZOut.PutNextEntry(ZipEntry)
    90. FS = New FileStream(InputFiles(i), FileMode.Open, FileAccess.Read, FileShare.Read)
    91. Do
    92. ByteLen = FS.Read(Buffer, 0, Buffer.Length)
    93. ZOut.Write(Buffer, 0, ByteLen)
    94. Loop While Not (ByteLen <= 0)
    95. FS.Close()
    96. Next
    97. ZOut.Finish()
    98. ZOut.Close()
    99. ZFS.Close()
    100. End Sub
    101. Public Sub DecompressFile(FileName As String, OutputDir As String)
    102. Dim ZFS As New FileStream(FileName, FileMode.Open)
    103. Dim ZIN As New ICSharpCode.SharpZipLib.Zip.ZipInputStream(ZFS)
    104. Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
    105. Dim Buffer As Byte() = New Byte(4096) {}
    106. Dim ByteLen As Integer = 0
    107. Dim FS As FileStream = Nothing
    108. Dim InZipDirName As String = Nothing
    109. Dim InZipFileName As String = Nothing
    110. Dim TargetFileName As String = Nothing
    111. Do
    112. ZipEntry = ZIN.GetNextEntry()
    113. If ZipEntry Is Nothing Then
    114. Exit Do
    115. End If
    116. InZipDirName = Path.GetDirectoryName(ZipEntry.Name) & ""
    117. InZipFileName = Path.GetFileName(ZipEntry.Name)
    118. If Directory.Exists(OutputDir & "" & InZipDirName) = False Then
    119. Directory.CreateDirectory(OutputDir & "" & InZipDirName)
    120. End If
    121. If InZipDirName = "" Then
    122. InZipDirName = ""
    123. End If
    124. TargetFileName = OutputDir & "" & InZipDirName & InZipFileName
    125. FS = New FileStream(TargetFileName, FileMode.Create)
    126. Do
    127. ByteLen = ZIN.Read(Buffer, 0, Buffer.Length)
    128. FS.Write(Buffer, 0, ByteLen)
    129. Loop While Not (ByteLen <= 0)
    130. FS.Close()
    131. Loop While True
    132. ZIN.Close()
    133. ZFS.Close()
    134. End Sub
    135. Private Sub GetAllFiles(Root As String, ByRef FileArray As List(Of String))
    136. Try
    137. Dim Files As String() = System.IO.Directory.GetFiles(Root)
    138. Dim Folders As String() = System.IO.Directory.GetDirectories(Root)
    139. For i As Integer = 0 To Files.Length - 1
    140. FileArray.Add(Files(i).ToString())
    141. Next
    142. For i As Integer = 0 To Folders.Length - 1
    143. GetAllFiles(Folders(i), FileArray)
    144. Next
    145. Catch Ex As Exception
    146. End Try
    147. End Sub
    148. End Class




    LG

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Keybladmaster“ ()

    /bin

    und jetzt schaust du dir mal nen Pfad im Explorer an ;)
    Zudem: Shell -> Process.Start() und lieber System.IO verwenden.
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Pfade haben einen Backslash, keinen normalen. Also \ statt /.
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:

    Keybladmaster schrieb:

    Hat einer eine Lösung für mein Problem?
    Schreib Dir einen rekursiven Algorithmus zum Kopieren, da siehst Du besser, was passiert.
    Gugst Du hier.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    NGE'o schrieb:

    weil sie geöffnet ist?
    Probier das mal im Explorer aus. ;)
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Trade schrieb:

    Pfade haben einen Backslash, keinen normalen. Also \ statt /.
    Das ist (unter Windows) egal. Unter Linux ist zwingend der normale Slash zu verwenden.

    Zum Thema "Zugriff verweigert": Wenn du auf irgendwelches System-Zeugs wie zum Beispiel SystemVolumeInformation zugreifen willst, wird dies verweigert. Welchen Pfad willst du kopieren? MessageBoxen sind keine Fehlerbehandlung! [VB.NET] TryCatch ist ein heißes Eisen
    Setz mal den Parameter /R hinzu,
    ich leg Dir mal das, winhelpline.info/forum/allgeme…copy-und-windows-7-a.html
    und support.microsoft.com/kb/149199/eng , bei.

    Nur eine kleine Frage zu Deinem MessageBox.Show(ErrorToString.ToString), ist ErrorToString nicht ein String -Doch, es findet also keine Konvertierung statt, und wenn Du das Err-Function nutzen willst , nimm doch,Err.GetException.ToString, oder lasse dem Debugger den Fehler Dir beschreiben, ist ja dafür da.
    Ich mach das so:

    VB.NET-Quellcode

    1. Dim ZielVerz As String = "\Meine-Sicherung" & Today.Year.ToString & Today.Month.ToString("00") & Today.Day.ToString("00")
    2. 'Kopiert DatDBVerz nach DatDaSiVerz (True: überschreibt bestehendes Verzeichnis)
    3. My.Computer.FileSystem.CopyDirectory(My.Settings.DatDBVerz, My.Settings.DatDaSiVerz & ZielVerz, True)
    Erstmal danke für die ganzen Antworten :thumbsup:

    Also mein Zielverzeichnis ist "LocalAppData" (Pfad weiß ich gerade nicht, bin mitn Handy on)

    Das mit der MessageBox etc. werde ich Berücksichtigen, Danke.

    Ich werde mal den Link hier Ausprobieren von Snaptu.
    vbarchiv.net/tipps/tipp_1597-o…rdner-kopieren-vbnet.html

    @Snaptu

    Ja, ich hätte das sowieso nicht weiter Verwendet auch wen es Funktioniert hätte.
    Aber trotzdem Danke, es war lediglich ein Test. ^^

    LG
    Da ja LocalAppData das selbe ist wie Appdata\Local könntest du auch mit Environment.SpecialFolder.LocalApplicationData arbeiten

    VB.NET-Quellcode

    1. Dim appdata As String = GetFolderPath(SpecialFolder.LocalApplicationData)
    2. Dim backuppath As String = Path.Combine(appdata, "backup")
    Wer fragt, ist ein Narr für eine Minute. Wer nicht fragt, ist ein Narr sein Leben lang.

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Snaptu“ ()

    Ich habe das ganze zeit so gemacht :

    VB.NET-Quellcode

    1. Dim C As String = Environment.ExpandEnvironmentVariables("%LocalAppData%")



    EDIT :

    Habe das nun mal so nach dem Link Probiert, aber leider kein Erfolg.
    Bekomme trotzdem den Fehler das der Zugriff verweigert wurde.

    LG

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Keybladmaster“ ()

    Hast Du im Explorer Zugriff auf dieses Verzeichnis?
    Hast Du Dich davon überzeugt, dass Du den richtigen Pfad verwendest?
    Brauchst Du ggf. Admin-Rechte?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Also bei mir gehts das wunderbar....

    VB.NET-Quellcode

    1. Dim source As String = "C:\test.txt"
    2. Dim appdata As String = GetFolderPath(SpecialFolder.LocalApplicationData)
    3. Dim backuppath As String = Path.Combine(appdata, "backup")
    4. File.Copy(source, backuppath & ".txt")
    Wer fragt, ist ein Narr für eine Minute. Wer nicht fragt, ist ein Narr sein Leben lang.

    Keybladmaster schrieb:

    Ich habe das ganze zeit so gemacht :

    VB.NET-Quellcode

    1. Dim C As String = Environment.ExpandEnvironmentVariables("%LocalAppData%")



    Wenn dir das System deinen "Wunschpfad" schon komplett gibt, warum nutzt du ihn dann nicht?
    RootFolder = Environment.SpecialFolder.LocalApplicationData