visualbasic cant delete .zip file in a directory - vb.net

i have three folder in "D:\TestField". These are "backups","ExampleFileUpdatePrimary","ExampleFileUpdateSecondary.zip". I can delete "D:\TestField\ExampleFileUpdatePrimary" folder ,but i cant delete "D:\TestField\ExampleFileUpdateSecondary.zip"
my sub function is ;
Private Sub DeleteFolder(ByVal path As String)
path = path.Remove(path.Length - 1) 'D:\TestField\ExampleFileUpdatePrimary
Dim path2 = path & ".zip" 'D:\TestField\ExampleFileUpdateSecondary.zip
My.Computer.FileSystem.DeleteDirectory(path, FileIO.DeleteDirectoryOption.DeleteAllContents) 'its working
'Dim di = New IO.DirectoryInfo(path2) 'try1
'di.Delete(True)
'IO.Directory.Delete(path2) 'try2
'My.Computer.FileSystem.DeleteDirectory(path2, FileIO.DeleteDirectoryOption.DeleteAllContents) 'try3
End Sub
try1,try2 exceptions says " The directory name is invalid"
try3 exception says "System.IO.DirectoryNotFoundException: ''D:\TestField\ExampleFileUpdateSecondary.zip "
i couldnot find a new solution or try to delete a .zip file in code
thanks

Directory entries ending on .zip are usually files not folders, even though Windows Explorer displays them as folders.
Since they are files, use File.Delete instead.

Related

Unable to delete files with full permissions

I coded a simple function that deletes all files in a directory. All files in my directory (including the directory) have full permission for all users but my function doesn't work and I receive this error: "Access to the path is denied."
Very important: This error appears only for the file that I have copied manually from another dir, instead for that files that my program creates and saves, there is no error.
This is my function:
Private Sub DeleteFile(ByVal Folder As IO.DirectoryInfo)
Dim _fl As IO.FileInfo
For Each _fl In Folder.GetFiles
_fl.Delete()
Next
_fl = Nothing
End Sub

Saving image from php id URL using webbrowser in VB.net

I am trying to save a image from webbrowser but can't find an answer for it.
Example of the problem is the URL is hxxp://domain[.]com/image/?img=1234 on webbrowser it shows the image and the source is <img src='hxxp://domain[.]com/image/?img=1234'>..
I was unable to save it using the My.Computer.Network.DownloadFile and MemoryStream(tClient.DownloadData methods.
In order to download the file, I will need to session cookie also?
How can I do this?
Any image that you see in the WebBrowser has already been downloaded to your computer. It is normally cached in the folder:
C:\Users\<username>\AppData\Local\Microsoft\Windows\Temporary Internet Files
or whatever other folder is set in Internet Options. You can get your image from that folder.
The following program shows how to copy file TDAssurance_Col.png from the Temporary Internet Files folder to the C:\Temp folder.
Module Module1
Sub Main()
CopyFileFromTemporaryInternetFolder("TDAssurance_Col.png", "C:\Temp")
End Sub
Public Sub CopyFileFromTemporaryInternetFolder(filename As String, destinationFolder As String)
' Search actual path of filename.
Dim temporaryInternetFilesFolder As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "Content.IE5")
Dim pattern As String = Path.GetFileNameWithoutExtension(filename) & "*" & Path.GetExtension(filename)
Dim pathnames() As String = Directory.GetFiles(temporaryInternetFilesFolder, pattern, SearchOption.AllDirectories)
' If file found, copy it.
If pathnames.Count > 0 Then
File.Copy(pathnames(0), Path.Combine(destinationFolder, filename))
End If
End Sub
End Module

VB.NET %appdata% path access is denied

I want a folder to be created inside user's %appdata% folder, it does create, but it cannot write a file inside the folder, it says "Access denied", I googled it, but nothing worked for me.
Dim SpecialAppData As String = GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim CLOUDSAVEDFILESCFG As String = SpecialAppData & "\da_dev\clientsettings\cloud\"
If System.IO.Directory.Exists(SpecialAppData & "\da_dev\clientsettings\cloud\") Then
Else
System.IO.Directory.CreateDirectory(SpecialAppData & "\da_dev\clientsettings\cloud\")
End If
timeout(0.5)
Dim objWriterCSF As New System.IO.StreamWriter(CLOUDSAVEDFILESCFG)
TextBox1.Text = My.Settings.username
TextBox2.Text = My.Settings.password
If My.Computer.FileSystem.FileExists(SpecialAppData & "\da_dev\clientsettings\cloud\" & "files.cfg") Then
Else
objWriterCSF.Write("0")
objWriterCSF.Close()
End If
The error is:
EDIT:
I noticed the picture says that it cannot find path, I fixed that, but now I get the issue:
You can not "open a file" that is actually a directory.
Dim CLOUDSAVEDFILESCFG As String = SpecialAppData & "\da_dev\clientsettings\cloud\"
CLOUDSAVEDFILESCFG points to a directory.
The error here is not that access is denied, but that the directory does not exist. You can use Directory.CreateDirectory(Path.GetDirectoryName(CLOUDSAVEFILESCFG)) to ensure that the directory exists before trying to create a file within it.
Solved:
Forgot to add a file behind CLOUDSAVEDFILESCFG...
Like this:
Dim objWriterCSF As New System.IO.StreamWriter(CLOUDSAVEDFILESCFG & "\files.cfg")

How to open file located inside winform project

I have a project that requires some pdf reports to be opened from my forms. Since the program can be installed in any drive, I used the winform location directory as my path. I reports are located on a directory called Reports. It works well under development becuase I the winform is under bin\debug, but when I deploy my solution and try the procedure, it does not work, the file cannot be found. I created a Reports directory after deployment, but still did not find my pdf.
Here is the code:
'Open the requested file name. The files are stored in the
'same path as the main workbook.
'#param filename file to be opened
Function OpenReports(strFileName As String) As String
Dim reportFolder As String
Try
reportFolder = Path.Combine(Directory.GetCurrentDirectory(), "Reports")
System.Diagnostics.Process.Start(reportFolder & "\" & strFileName)
Catch ex As Exception
MsgBox("The " & strFileName & " is not found on directory")
End Try
Return strFileName
End Function
Private Sub btnRptRangesToMarket_Click(sender As Object, e As EventArgs) Handles btnRptRangesToMarket.Click
'This procedure runs when the btnRptRangesToMarket is clicked
'the procedure opens the pdf below by running the OpenReports Function
OpenReports("Ranges to Market.pdf")
End Sub
Try Application.StartupPath instead of Directory.GetCurrentDirectory(). I always get good results with that property.
This is from MSDN:
The current directory is distinct from the original directory, which is the one from which the process was started.

VB.net Save File Dialogue error - Could not find special directory 'Desktop'

I have a fairly straight forward peice of code that just tries to set the default saved directory for a standard .net save dialogue to a specific folder. If that folder doesn't exist, it sets it to the desktop.
This works fine for everyone but one user who is getting the following error:
Could not find special directory 'Desktop'
How is that even possible?
'Check if folder exists
If Not IO.Directory.Exists(strDirectory) Then
strDirectory = FileIO.SpecialDirectories.Desktop
If Not IO.Directory.Exists(strDirectory) Then
strDirectory = IO.Directory.GetCurrentDirectory
End If
End If
'Show save file dialogue.
Dim folderDlg As New Windows.Forms.FolderBrowserDialog
folderDlg.RootFolder = Environment.SpecialFolder.Desktop
folderDlg.SelectedPath = strDirectory
folderDlg.ShowNewFolderButton = True
How about:
strDirectory = _
Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString()
I use GetFolderPath() to get "My Documents" and it works fine (I don't ever have to think about it).