Hi
Ich habe ein kleines Programm geschrieben das Dateien downloaden kann. Nur mein Problem ist die gedownloadeten Dateien werden als "pure" Dateien dargestellt. Also als Dateien ohne Dateinamenserweiterung das Icon dieser Dateien ist einfach ein weißes Blatt mit einer Knickecke oben rechts.
Meine Frage nun was muss ich an diesem Code ändern so das die Datei im MP3-Format gespeichert wird (mit dem Filter beim SaveFileDialog hab es schon versucht, hat nicht funktioniert)
Der Code:
Spoiler anzeigen
Möglicherweise wichtige angaben: "Me.loc" ist die Textbox mit dem Speicherort. "Me.sloc" ist der Label mit dem Inhalt von "Me.loc".
Ich habe ein kleines Programm geschrieben das Dateien downloaden kann. Nur mein Problem ist die gedownloadeten Dateien werden als "pure" Dateien dargestellt. Also als Dateien ohne Dateinamenserweiterung das Icon dieser Dateien ist einfach ein weißes Blatt mit einer Knickecke oben rechts.
Meine Frage nun was muss ich an diesem Code ändern so das die Datei im MP3-Format gespeichert wird (mit dem Filter beim SaveFileDialog hab es schon versucht, hat nicht funktioniert)
Der Code:
VB.NET-Quellcode
- Imports System.Net
- Public Class Form1
- Public Const Clickdown As Integer = &HA1
- Public Const FormCapture As Integer = &H2
- Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
- Private Declare Function ReleaseCapture Lib "user32.dll" () As Int32
- Dim whereToSave As String
- Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
- Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
- Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
- If (e.Button = Windows.Forms.MouseButtons.Left) Then
- ReleaseCapture()
- SendMessage(Handle.ToInt32, Clickdown, FormCapture, 0)
- End If
- End Sub
- Public Sub DownloadComplete(ByVal cancelled As Boolean)
- Me.txtFileName.Enabled = True
- Me.btn_download.Enabled = True
- If cancelled Then
- Me.btn_cancel.Enabled = False
- Me.lblstat.Text = "Status : " & "Abgebrochen"
- MessageBox.Show("Download wurde abgebrochen !", "Abgebrochen", MessageBoxButtons.OK, MessageBoxIcon.Information)
- Else
- Me.btn_cancel.Enabled = False
- Me.lblstat.Text = "Status : " & "Successfully downloaded"
- MessageBox.Show("Erfolgreich heruntergeladen !", "Fertig", MessageBoxButtons.OK, MessageBoxIcon.Information)
- End If
- Me.ProgressBar1.Value = 0
- End Sub
- Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
- Me.lblsize.Text = "Größe: " & Math.Round((length / 1024), 2) & " KB"
- Me.lbldownloading.Text = "Downloading: " & Me.txtFilename.Text
- Me.lblstat.Text = "Status: " & Math.Round((position / 1024), 2) & " KB von " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
- Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
- If speed = -1 Then
- Me.lblspd.Text = "Geschwindigkeit: " & "wird ausgerechnet..."
- Else
- Me.lblspd.Text = "Geschwindigkeit: " & Math.Round((speed / 1024), 2) & " KB/s"
- End If
- Me.ProgressBar1.Value = percent
- End Sub
- Private Sub btn_download_Click(sender As System.Object, e As System.EventArgs) Handles btn_download.Click
- If Me.txtFilename.Text <> "" AndAlso Me.txtFilename.Text.StartsWith("http://") Then
- Me.whereToSave = Me.loc.Text
- Me.SaveFileDialog1.FileName = ""
- Me.lblsloc.Text = "Speicherort: " & whereToSave
- Me.txtFilename.Enabled = False
- Me.btn_download.Enabled = False
- Me.btn_cancel.Enabled = True
- Me.loc.Enabled = False
- Me.brws.Enabled = False
- Me.BackgroundWorker1.RunWorkerAsync()
- Else
- MessageBox.Show("Dieser Link verweist auch kein gültiges Videoformat", "Warnung", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- End If
- End Sub
- Private Sub brws_Click(sender As System.Object, e As System.EventArgs) Handles brws.Click
- Me.SaveFileDialog1.FileName = Me.txtFilename.Text.Split("/"c)(Me.txtFilename.Text.Split("/"c).Length - 1)
- Me.lblname.Text = "Name: " & Me.txtFilename.Text.Split("/"c)(Me.txtFilename.Text.Split("/"c).Length - 1)
- Me.SaveFileDialog1.ShowDialog()
- Me.loc.Text = Me.SaveFileDialog1.FileName
- End Sub
- Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
- Me.btn_download.Enabled = False
- Dim theResponse As HttpWebResponse
- Dim theRequest As HttpWebRequest
- Try
- theRequest = WebRequest.Create(Me.txtFilename.Text)
- theResponse = theRequest.GetResponse
- Catch ex As Exception
- MessageBox.Show("Ein Fehler ist aufgetreten während sie die Datei(en) gedownloadet haben. Mögliche Ursachen:" & ControlChars.CrLf & _
- "1) Die Datei existiert n" & ControlChars.CrLf & _
- "2) Webserver-Fehler, versuchen sie es später nochmal", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
- Me.Invoke(cancelDelegate, True)
- Exit Sub
- End Try
- Dim length As Long = theResponse.ContentLength
- Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
- Me.Invoke(safedelegate, length, 0, 0, 0)
- Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
- Dim nRead As Integer
- Dim speedtimer As New Stopwatch
- Dim currentspeed As Double = -1
- Dim readings As Integer = 0
- Do
- If BackgroundWorker1.CancellationPending Then
- Exit Do
- End If
- speedtimer.Start()
- Dim readBytes(4095) As Byte
- Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
- nRead += bytesread
- Dim percent As Short = (nRead / length) * 100
- Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
- If bytesread = 0 Then Exit Do
- writeStream.Write(readBytes, 0, bytesread)
- speedtimer.Stop()
- readings += 1
- If readings >= 5 Then
- currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
- speedtimer.Reset()
- readings = 0
- End If
- Loop
- theResponse.GetResponseStream.Close()
- writeStream.Close()
- If Me.BackgroundWorker1.CancellationPending Then
- IO.File.Delete(Me.whereToSave)
- Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
- Me.Invoke(cancelDelegate, True)
- Exit Sub
- End If
- Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
- Me.Invoke(completeDelegate, False)
- End Sub
- End Class
Möglicherweise wichtige angaben: "Me.loc" ist die Textbox mit dem Speicherort. "Me.sloc" ist der Label mit dem Inhalt von "Me.loc".