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
Related
I would like to move the folder and its contents to another directory. This means the folder and contents will no longer exist from the source and will be moved to its destination. But when the destination path has the same folder name, it will just overwrite the folder and its content.
I'm getting error if same folder exists in the destination path using the code below.
The catch here is I don't exactly know the folder name and filename here inside the source path. I just know where I'm going to get the folder and files to move to its destination.
I've used these codes but couldn't figure out how to overwrite:
'web.config
Sourcepath = "C:\TestSource\"
Destinationpath = "C:TestDestiation\"
'calling the move function:
dInfoSource = New DirectoryInfo(ccConfigClass.SourcePath)
dInfoDestination = New DirectoryInfo(ccConfigClass.DestinationPath)
MoveContents(dInfoSource, dInfoDestination)
Public Sub MoveContents(sourcePath As DirectoryInfo, destinationPath As DirectoryInfo)
Try
If (Not destinationPath.Exists) Then
destinationPath.Create()
End If
'Move all Files
For Each file As FileInfo In sourcePath.GetFiles()
file.MoveTo(Path.Combine(destinationPath.FullName, file.Name))
Next
'Move all folders
For Each dir As DirectoryInfo In sourcePath.GetDirectories()
dir.MoveTo(Path.Combine(destinationPath.FullName, dir.Name))
Next
Catch ex As Exception
Dim objErrorLogger As New ErrorLogging
objErrorLogger.LogError(_CLASS_NAME, Reflection.MethodInfo.GetCurrentMethod.Name, ex.Message.ToString, ex)
End Try
End Sub
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.
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
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.
Is there a way, in VB.NET, to check if a URL is a directory? I've seen a lot of methods of checking if a local path is a directory but what about a remote url (i.e. http://website.com/foo) I read that some plain text files have no extension so I need a solution other than checking what if the file name contains a space or something.
You can use FileAttributes class:
'get the file attributes for file or directory
FileAttributes attr = File.GetAttributes("c:\\Temp")
'detect whether its a directory or file
If ((attr & FileAttributes.Directory) = FileAttributes.Directory) Then
MessageBox.Show("Its a directory")
Else
MessageBox.Show("Its a file")
End IF
Or you can use the Uri class:
Private IsLocalPath(Byval p As String) As Boolean
Return New Uri(p).IsFile
End Function
You can enhance this method to include support for certain invalid URIs:
Private IsLocalPath(Byval p As String) As Boolean
If (p.StartsWith("http:\\")) Then
Return False
End IF
Return New Uri(p).IsFile
End Function
The only solution I can think of is to try to download the file from the Internet, if the download succeeded So it is a file, otherwise it is not a file (but you don't know for sure that this is a directory).
This worked for me...
If System.IO.Path.HasExtension(FileAddress.Text) Then
MessageBox.Show("Its a file")
Else
MessageBox.Show("Its a directory")
End IF