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.
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!
Code zur ZipLib.vb (Klasse)
LG
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
- try
- My.Computer.FileSystem.CopyDirectory(ordner, My.Application.Info.DirectoryPath & "/bin", True)
- Catch ex As Exception
- MessageBox.Show(ErrorToString.ToString())
- end try
- 'Habe es auch so Versucht aber auch selbes Resultat
- Try
- Dim C As String = Environment.ExpandEnvironmentVariables("%LocalAppData%")
- Shell("xcopy " & C & " " & My.Application.Info.DirectoryPath & "/bin/backup/section1" & " /E /C /Q /I /Y")
- Catch ex As Exception
- MessageBox.Show(ErrorToString.ToString)
- 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 :
Ein Verweis auf die DLL muss da sein und die Klasse auch!
VB.NET-Quellcode
- 'Erstellen eines Ordners und des Pfades
- System.IO.Directory.CreateDirectory("Dein Pfad")
- Dim directorypath as String = Environment.SpecialFolder.LocalApplicationData
- 'Zwar nicht überall gesehen aber hier ein muss (für die Dateien die Verwendet werden oder Kopier Geschütz sind)
- On Error Resume Next
- 'ZipLib Initialisieren und Zip erstellen
- Dim ZipLib As New ZipLib
- ZipLib.CompressionLevel = 6 'Kein Muss
- ZipLib.CompressDirectory(ordner, "namederZip.zip", "/ordner_wo_es_gespeichert_werden_soll") 'das .zip muss hinten dran!!
- 'Entpacken
- ZipLib.DecompressFile("File.zip","/ordner_wo_es_gespeichert_werden_soll")
Code zur ZipLib.vb (Klasse)
VB.NET-Quellcode
- Imports System.IO
- Imports System.Collections
- Imports System.Collections.Generic
- Imports System.Data
- Public Class ZipLib
- Private mCompressionLevel As Integer = 6
- Public Property CompressionLevel() As Integer
- Get
- Return mCompressionLevel
- End Get
- Set(value As Integer)
- mCompressionLevel = value
- End Set
- End Property
- Public Sub CompressDirectory(InputDir As String, FileName As String, OutputDir As String)
- Dim Files As New List(Of String)()
- Dim RelativePath As String = Nothing
- GetAllFiles(InputDir, Files)
- If String.IsNullOrEmpty(OutputDir) Then
- OutputDir = Path.GetDirectoryName(InputDir)
- End If
- If Directory.Exists(OutputDir) = False Then
- Directory.CreateDirectory(OutputDir)
- End If
- Dim ZFS As New FileStream(OutputDir & "" & FileName, FileMode.Create)
- Dim ZOut As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ZFS)
- ZOut.SetLevel(6)
- Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
- Dim Buffer As Byte() = New Byte(4096) {}
- Dim ByteLen As Integer = 0
- Dim FS As FileStream = Nothing
- Dim ParentDirLen As Integer = InputDir.Length + 1
- For i As Integer = 0 To Files.Count - 1
- RelativePath = Files(i).Substring(ParentDirLen)
- ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(RelativePath)
- ZipEntry.DateTime = System.DateTime.Now
- ZOut.PutNextEntry(ZipEntry)
- FS = New FileStream(Files(i), FileMode.Open, FileAccess.Read, FileShare.Read)
- Do
- ByteLen = FS.Read(Buffer, 0, Buffer.Length)
- ZOut.Write(Buffer, 0, ByteLen)
- Loop While Not (ByteLen <= 0)
- FS.Close()
- Next
- ZOut.Finish()
- ZOut.Close()
- ZFS.Close()
- End Sub
- Public Sub CompressDirectory(InputFiles As List(Of String), FileName As String, OutputDir As String)
- Dim RelativePath As String = Nothing
- If Directory.Exists(OutputDir) = False Then
- Directory.CreateDirectory(OutputDir)
- End If
- Dim ZFS As New FileStream(OutputDir & "" & FileName, FileMode.Create)
- Dim ZOut As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ZFS)
- ZOut.SetLevel(6)
- Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
- Dim Buffer As Byte() = New Byte(4096) {}
- Dim ByteLen As Integer = 0
- Dim FS As FileStream = Nothing
- Dim ParentDirLen As Integer = Path.GetDirectoryName(InputFiles(0)).Length
- For i As Integer = 0 To InputFiles.Count - 1
- RelativePath = InputFiles(i).Substring(ParentDirLen)
- ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(RelativePath)
- ZipEntry.DateTime = System.DateTime.Now
- ZOut.PutNextEntry(ZipEntry)
- FS = New FileStream(InputFiles(i), FileMode.Open, FileAccess.Read, FileShare.Read)
- Do
- ByteLen = FS.Read(Buffer, 0, Buffer.Length)
- ZOut.Write(Buffer, 0, ByteLen)
- Loop While Not (ByteLen <= 0)
- FS.Close()
- Next
- ZOut.Finish()
- ZOut.Close()
- ZFS.Close()
- End Sub
- Public Sub CompressFiles(InputFiles As List(Of String), FileName As String, OutputDir As String)
- Dim ZFS As New FileStream(OutputDir & "" & FileName, FileMode.Create)
- Dim ZOut As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ZFS)
- ZOut.SetLevel(6)
- Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
- Dim Buffer As Byte() = New Byte(4096) {}
- Dim ByteLen As Integer = 0
- Dim FS As FileStream = Nothing
- For i As Integer = 0 To InputFiles.Count - 1
- ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(InputFiles(i)))
- ZipEntry.DateTime = System.DateTime.Now
- ZOut.PutNextEntry(ZipEntry)
- FS = New FileStream(InputFiles(i), FileMode.Open, FileAccess.Read, FileShare.Read)
- Do
- ByteLen = FS.Read(Buffer, 0, Buffer.Length)
- ZOut.Write(Buffer, 0, ByteLen)
- Loop While Not (ByteLen <= 0)
- FS.Close()
- Next
- ZOut.Finish()
- ZOut.Close()
- ZFS.Close()
- End Sub
- Public Sub DecompressFile(FileName As String, OutputDir As String)
- Dim ZFS As New FileStream(FileName, FileMode.Open)
- Dim ZIN As New ICSharpCode.SharpZipLib.Zip.ZipInputStream(ZFS)
- Dim ZipEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = Nothing
- Dim Buffer As Byte() = New Byte(4096) {}
- Dim ByteLen As Integer = 0
- Dim FS As FileStream = Nothing
- Dim InZipDirName As String = Nothing
- Dim InZipFileName As String = Nothing
- Dim TargetFileName As String = Nothing
- Do
- ZipEntry = ZIN.GetNextEntry()
- If ZipEntry Is Nothing Then
- Exit Do
- End If
- InZipDirName = Path.GetDirectoryName(ZipEntry.Name) & ""
- InZipFileName = Path.GetFileName(ZipEntry.Name)
- If Directory.Exists(OutputDir & "" & InZipDirName) = False Then
- Directory.CreateDirectory(OutputDir & "" & InZipDirName)
- End If
- If InZipDirName = "" Then
- InZipDirName = ""
- End If
- TargetFileName = OutputDir & "" & InZipDirName & InZipFileName
- FS = New FileStream(TargetFileName, FileMode.Create)
- Do
- ByteLen = ZIN.Read(Buffer, 0, Buffer.Length)
- FS.Write(Buffer, 0, ByteLen)
- Loop While Not (ByteLen <= 0)
- FS.Close()
- Loop While True
- ZIN.Close()
- ZFS.Close()
- End Sub
- Private Sub GetAllFiles(Root As String, ByRef FileArray As List(Of String))
- Try
- Dim Files As String() = System.IO.Directory.GetFiles(Root)
- Dim Folders As String() = System.IO.Directory.GetDirectories(Root)
- For i As Integer = 0 To Files.Length - 1
- FileArray.Add(Files(i).ToString())
- Next
- For i As Integer = 0 To Folders.Length - 1
- GetAllFiles(Folders(i), FileArray)
- Next
- Catch Ex As Exception
- End Try
- End Sub
- End Class
LG
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Keybladmaster“ ()