Datei kopieren, Zugriff verweigert

  • VB.NET

Es gibt 21 Antworten in diesem Thema. Der letzte Beitrag () ist von ThePlexian.

    Datei kopieren, Zugriff verweigert

    Hey Leute!

    Ich progge momentan nen LogOn Screen Changer, und hab folgendes Problem:
    Also in der Form ist ne PictureBox, in der das Bild, was vorher in nem OpenFileDialog() ausgewählt wurde, angezeigt wird, oder halt falls noch nichts ausgewählt wurde, der akutelle Background.
    Damit das funktioniert muss das Bild ein JPEG sein und < 256 kB. Also hab ich ein If-Statement erstellt:

    VB.NET-Quellcode

    1. Dim picturesizeKB As Single = Math.Round(My.Computer.FileSystem.GetFileInfo(imagepath).Length / 1024, 2) 'Size in Bytes - Divided by 1024 -> KiloByte
    2. Dim filename As String = My.Computer.FileSystem.GetFileInfo(imagepath).Name 'Name of the file, i.e. "ABCDEFG.bmp"
    3. Dim fileformat As String = filename.Substring(filename.IndexOf(".") + 1) 'Locates the dot in the name and returns all chars behind it --> "bmp"
    4. If fileformat <> "jpg" And picturesizeKB > 256 Then
    5. MsgBox(info1, MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Size and Format Exception") 'info1 is a String with the Error "Worng Fileformat and Size"
    6. If MsgBoxResult.Ok = 1 Then
    7. My.Computer.FileSystem.CopyFile(imagepath, destinationpath & "backgroundDefault.jpg", True) ' Copy, Rename, Convert File
    8. MsgBox("Modification successful" & vbCrLf & picturesizeKB & ps)
    9. End If
    10. ElseIf picturesizeKB > 256 Then
    11. ElseIf fileformat <> "jpg" Then
    12. Else
    13. End If


    Das ist zwar nur ein Ausschnitt, sollte aber denke ich ausreichen. Das Problem ist jetzt, dass das Prog stoppt an der Stelle, wo die Datei hinkopiert werden muss, mit der Begründung "Der Prozess kann nicht auf die Datei "C:\Users\*****\Desktop\backgroundDefault.jpg" zugreifen, da sie von einem anderen Prozess verwendet wird." Diese Meldung kommt aber nur, wenn die Datei bereits existiert :/
    Ich hoffe ihr könnt mir helfen, DANKE !
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    1.) Weg von My.Computer.FileSystem.*
    Ersatz:

    VB.NET-Quellcode

    1. Dim fi As New FileInfo("DeineDatei")
    2. '========= Dateiinfos =========
    3. Dim kb As Integer = CInt(fi.Lenght / 1024, 0) 'Größe in kb
    4. Dim fname As String = fi.Name 'Dateiname
    5. Dim fformat As String = fi.Extension.Replace(".", "") 'Erweiterung
    6. fi.CopyTo("Ziel", True) 'Kopieren nach "Ziel" (True, dass eine vorhandene Datei überschrieben wird)
    Grüße,
    Lukas

    Fragen über Themen im Forum per Konversation werden gelöscht und die Absender blockiert...
    Ich habs geändert, aber leider immer noch das gleiche Problem :/

    Also nochmal zum Verständnis:
    Das Bild an sich wird in einer PictureBox angezeigt, und wurde vorher in nem OpenFileDialog ausgewählt.
    Und wenn der Zielpfad bereits existiert, nur dann, kommt der Fehler :/

    VB.NET-Quellcode

    1. Dim fi As New IO.FileInfo(imagepath)
    2. Dim picturesize As Single = Math.Round(fi.Length / 1024, 2) 'Size in Bytes - Divided by 1024 -> KiloByte
    3. Dim filename As String = fi.Name 'Name of the file, i.e. "ABCDEFG.bmp"
    4. Dim fileformat As String = fi.Extension.Replace(".", "") 'Deletes the dot in the Extension
    5. 'If fileformat <> "jpg" And picturesize > 256 Then
    6. 'ElseIf picturesize > 256 Then
    7. 'ElseIf fileformat <> "jpg" Then
    8. 'Else
    9. fi.CopyTo(destinationpath & "backgroundDefault.jpg", True)
    10. PictureInfo.Text = "Current LogOn Screen"
    11. MsgBox("Modification successful")
    12. 'End If
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Okay ich bin grad ein bisschen durcheinander:

    Also der Teil den ich postet hab soll so bleiben, nur soll vorm kopieren ein Dispose rein.
    Und "disposet" wird ein Streamreader, den ich da konfiguriert habe, wo ich den Pfad für die PictureBox angebe ? ?(

    Also der Teil sieht so aus:

    VB.NET-Quellcode

    1. If System.IO.File.Exists(destinationpath & "backgroundDefault.jpg") Then
    2. Picture.Image = New Bitmap(destinationpath & "backgroundDefault.jpg")
    3. imagepath = destinationpath & "backgroundDefault.jpg"
    4. Else
    5. Picture.Image = New Bitmap("C:\Windows\System32\oobe\background.bmp")
    6. imagepath = destinationpath.Replace("info\backgrounds\", "") & "background.bmp" '"C:\Windows\System32\oobe\background.bmp"
    7. End If


    Sorry, dass ich mich so doof anstelle, ich fang grad erst an mich in VB einzuarbeiten :D

    EDIT: Und davor soll jetzt ein Streamreader ? 8|
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Ne... Vergiss den StreamReader, das war ein Versehen von mir... Der ist ja nur da um Textdateien auszuelsen :rolleyes:
    Gib das Bild doch vorm Kopieren einfach frei.

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

    Ja, aber das hab ich schon probiert, nur das bringt nichts :/
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais

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

    EDIT: Immer noch das "wird von einem anderen Prozess verwendet" - Problem :(
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais

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

    Hallo,

    hast du das Programm als Administrator gestartet, weil in den System32-Ordner
    kommt man ohne Administratorrechte nicht rein und darum vielleicht "Zugriff verweigert".

    MFG

    Alex-Digital :D
    ~ Alex-Digital :D

    if(!Internet.VBP.Get<User>("Alex-Digital").IsOnline) this.Close(); :D
    @Alex-Digital: Lies doch das Problem, bevor du Antworten gibst zu denen ich keine Frage gestellt hab ;)

    @programmer71:
    Es bringt einfach nichts :/ Ich habe es eingesetzt, vor dem Copy-Befehl, aber es besteht das selbe Problem :/

    Hier mal mein ganzer Code wenn ihr wollt: ;)

    DANKE programmer71 für deine Geduld ;)



    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.Drawing.Imaging
    3. Imports System.Drawing.Drawing2D
    4. Imports Microsoft.VisualBasic
    5. Public Class Form1
    6. Public imagepath As String = ""
    7. Public destinationpath As String = "C:\Users\" & Environment.UserName & "\Music\oobe\info\backgrounds\" '"C:\Windows\System32\oobe\info\backgrounds\"
    8. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    9. Picture.SizeMode = PictureBoxSizeMode.StretchImage
    10. If System.IO.File.Exists(destinationpath & "backgroundDefault.jpg") Then
    11. Picture.Image = New Bitmap(destinationpath & "backgroundDefault.jpg")
    12. imagepath = destinationpath & "backgroundDefault.jpg"
    13. Else
    14. Picture.Image = New Bitmap("C:\Windows\System32\oobe\background.bmp")
    15. imagepath = destinationpath.Replace("info\backgrounds\", "") & "background.bmp" '"C:\Windows\System32\oobe\background.bmp"
    16. End If
    17. Picture.Dispose()
    18. End Sub
    19. Private Sub ButtonBrowse_Click(sender As Object, e As EventArgs) Handles Browse.Click 'Search for the new Pic
    20. Dim FOInterface As New OpenFileDialog() 'Interface for Opening Files
    21. Try
    22. With FOInterface
    23. .InitialDirectory = "C:\Users\Public\Pictures\Computer\Backgrounds\" '"C:\Users\" & Environment.UserName & "\Pictures" 'Start Directory"
    24. .Filter = _
    25. "Alle Bilddateien|*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.png;*.tif;*.tiff|" & _
    26. "Alle Dateien (*.*)|*.*"
    27. .ShowDialog() 'Refresh()
    28. End With
    29. Picture.Image = New Bitmap(FOInterface.FileName)
    30. imagepath = FOInterface.FileName
    31. PictureInfo.Text = "Optional New LogOn Screen"
    32. Catch ex As Exception
    33. End Try
    34. End Sub
    35. Private Sub Restore_Click(sender As Object, e As EventArgs) Handles Restore.Click 'Set the LogOn Screen back to Default
    36. Picture.Image = New Bitmap("C:\Windows\System32\oobe\background.bmp")
    37. imagepath = "C:\Windows\System32\oobe\background.bmp"
    38. PictureInfo.Text = "Optional New LogOn Screen"
    39. End Sub
    40. Private Sub Change_Click(sender As Object, e As EventArgs) Handles Change.Click
    41. If imagepath <> "C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg" Then
    42. Dim regpath As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background"
    43. 'Try
    44. 'My.Computer.Registry.LocalMachine.OpenSubKey(regpath, True).CreateSubKey(regpath).SetValue("OEMBackground", "0", Microsoft.Win32.RegistryValueKind.DWord)
    45. 'Catch ex As Exception
    46. 'MsgBox(ex.Message)
    47. 'End Try
    48. Dim fi As New FileInfo(imagepath)
    49. Dim picturesize As Single = Math.Round(fi.Length / 1024, 2) 'Size in Bytes - Divided by 1024 -> KiloByte
    50. Dim filename As String = fi.Name 'Name of the file, i.e. "ABCDEFG.bmp"
    51. Dim fileformat As String = fi.Extension.Replace(".", "") 'Deletes the dot in the Extension
    52. 'If fileformat <> "jpg" And picturesize > 256 Then
    53. 'ElseIf picturesize > 256 Then
    54. 'ElseIf fileformat <> "jpg" Then
    55. 'Else
    56. File.Copy(imagepath, destinationpath & "backgroundDefault.jpg", True)
    57. PictureInfo.Text = "Current LogOn Screen"
    58. MsgBox("Modification successful")
    59. 'End If
    60. End If
    61. End Sub
    62. REM *********************************
    63. REM ***** Picture Changing Subs *****
    64. REM *********************************
    65. Sub ResizeImage(ByVal ipath As String, ByVal dpath As String, ByVal w As Integer, ByVal h As Integer)
    66. Dim i As Image = New Bitmap(ipath)
    67. Dim newImage As Image = New Bitmap(w, h)
    68. Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
    69. graphicsHandle.InterpolationMode = InterpolationMode.Default
    70. graphicsHandle.DrawImage(i, 0, 0, w, h)
    71. End Using
    72. i.Dispose()
    73. newImage.Save(dpath)
    74. End Sub
    75. Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
    76. Dim j As Integer
    77. Dim encoders() As ImageCodecInfo
    78. encoders = ImageCodecInfo.GetImageEncoders()
    79. j = 0
    80. While j < encoders.Length
    81. If encoders(j).MimeType = mimeType Then
    82. Return encoders(j)
    83. End If
    84. j += 1
    85. End While
    86. Return Nothing
    87. End Function[/spoiler]
    88. Public Shared Sub ChangeColorDepth(ByVal ipath As String, ByVal dpath As String)
    89. Dim myBitmap As Bitmap
    90. Dim myImageCodecInfo As ImageCodecInfo
    91. Dim myEncoder As Encoder
    92. Dim myEncoderParameter As EncoderParameter
    93. Dim myEncoderParameters As EncoderParameters
    94. myBitmap = New Bitmap(ipath)
    95. myImageCodecInfo = GetEncoderInfo("image/jpeg")
    96. myEncoder = Encoder.ColorDepth
    97. myEncoderParameters = New EncoderParameters(1)
    98. myEncoderParameter = New EncoderParameter(myEncoder, CType(24L, Int32))
    99. myEncoderParameters.Param(0) = myEncoderParameter
    100. myBitmap.Save(dpath, myImageCodecInfo, myEncoderParameters)
    101. End Sub
    102. End Class
    [/expander]
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    @ThePlexian
    Du benötigst nen FileStream keinen StreamReader, ich Depp! :D
    Versuchs mal damit. Wobei ich es gerade getestet habe und festgestellt habe, dass es nicht geht.
    Drum habe ich dir jetzt keinen Code hier hingeschrieben, da er dir eh nichts nutzen würde...
    Von daher tut es mir Leid dir das mitzuteilen, aber es gibt keine Lösung dafür...
    Du musst deinen Ansatz komplett überdenken...
    Sorry... Ich hoffe ich konnte dir trotzdem ein wenig helfen...

    Also was auch immer du versuchst es geht net... Nicht mal mit Dispose, wie ich gerade feststellen musste...

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „programmer71“ () aus folgendem Grund: Rechtschreibfehler

    ThePlexian schrieb:

    Also in der Form ist ne PictureBox, in der das Bild, was vorher in nem OpenFileDialog() ausgewählt wurde, angezeigt wird
    Wie lädst du die Daten in die Picturebox?
    Mit FromFile?
    Das blockiert die Datei.

    Mach den Umweg über FromStream:

    VB.NET-Quellcode

    1. Dim fs = New System.IO.FileStream("C:\xyz\abc.jpg", IO.FileMode.Open, IO.FileAccess.Read)
    2. PictureBox1.Image = System.Drawing.Image.FromStream(fs)
    3. fs.Close()
    Danach ist die Datei wieder frei.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    @petaod
    Hab ich getestet. Das funktioniert nicht. Den Code hab ich auch schon versucht. Das ist total übel. :/
    Selbst wenn du den FileStream wieder schließt wird der Zugriff verweigert. Teste es mal mit seinem Code...
    Darum hab ich meinen Beitrag auch wieder editiert, weil der überarbeitete Code von ihm dann mit FileStream auch nicht gefunzt hätte.

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