In vs2010, there is a vb.net project, I need to clean a folder before use it,
what I did is remote the folder, then create it again.
but the problem is ,it is removed but not created again.
I think maybe .net did some optimization. So how can I fix that?
Here is the code:
' delete folder
If IO.Directory.Exists(exportBaseFolder) = True Then
IO.Directory.Delete(exportBaseFolder, True)
End If
' create folder
IO.Directory.CreateDirectory(exportBaseFolder)
You could try Directory.GetFiles and foreach file, file.delete. Then delete the folder. Then if not folder exists, create it.
Related
I have following code that takes a local directory and copies it with a temporary name on a network drive.
Once there, it Waits a moment, deletes the original folder, and then renames the previously uploaded folder to the proper name.
All steps work fine, except for the last one. When it tries to rename the folder it throws an error saying that I do not have the permissions needed. Why? The folder is created by the program, it can delete the other folder just fine, why wouldnt it have the rights to rename it? Is it somehow "busy" or "used"? Any idea on how to fix this problem?
It happensonly from time to time, not every time.
copy("c:\somefolder\", "Y:\someotherfolder\temporary folder\")
System.Threading.Thread.Sleep(3000)
Directory.Delete("Y:\someotherfolder\original name\" , True)
System.Threading.Thread.Sleep(1000)
Directory.Move("Y:\someotherfolder\temporary folder\", "Y:\someotherfolder\original name\") ''this line causes trouble
thank you!
I'm making a VB application and I want to copy a file called "L3nEncrypt.jar" which is in resources of the application to the temp folder.
Right now I have this:
My.Computer.FileSystem.SpecialDirectories.Temp
which returns the temp folder.
I found the solution
Imports System.IO
File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Temp & "/L3nEncrypt.jar", My.Resources.L3nEncrypt)
Use System.IO.File.Copy(SourceFileName,DestinationFileName)
The parameters are full file names of type string. Make sure that you're running the application with admin privileges so that the copying can be successful.
This question is bring from another forum which not having answer yet for my situation.
I have something to do on network shared folder. But when I search on Internet, it giving me a code to do in own computer only. Step that I want to do is:
Check destination (network shared folder) path is empty or not.
Delete folder content (not the main one) eg: "\USER-PC\File\"; the folder "File" no need to deleted, but the content inside is need to deleted.
Copy folder content from source to new destination.
No. 1 and 3 is OK. But No. 2 is not yet found. How to delete a content from directory on Network Shared Folder?
Delete directory code that I use but exception "Could not complete operation since directory is a root directory":
My.Computer.FileSystem.DeleteDirectory(strDestination, FileIO.DeleteDirectoryOption.DeleteAllContents)
Please assist
EDITED:
To delete all files inside the main directory:-
Dim directory As New DirectoryInfo(strDestination)
For Each file As FileInfo In directory.GetFiles()
file.Delete()
Next file
To delete all folders inside the main directory:-
For Each folder As DirectoryInfo In directory.GetDirectories()
folder.Delete(True)
Next folder
Use this instead (it's C#, you'll need to convert it to VB.NET):
DirectoryInfo directory = new DirectoryInfo("\\USER-PC\File");
foreach(FileInfo file in directory.GetFiles()) {
file.Delete();
}
My application can create a directory, put a file in it from a ZIP file and then remove the ZIP file.
When I then try to delete that file, I get an Access Denied error even though nothing was done with it.
Here is the code:
File.WriteAllBytes(OpslagLocatieDocumenten + myTicketNummer.ToString + "\documenten.zip", op.Documenten)
Using zp As New ZipFile(OpslagLocatieDocumenten + myTicketNummer.ToString + "\documenten.zip")
zp.FlattenFoldersOnExtract = True
zp.ExtractAll(OpslagLocatieDocumenten + myTicketNummer.ToString, ExtractExistingFileAction.OverwriteSilently)
zp.Dispose()
End Using
Try
For Each itm As String In Directory.GetFiles(OpslagLocatieDocumenten + myTicketNummer.ToString)
Try
File.Delete(itm)
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
It looks a bit messy right now, but that is for testing purposes.
In the for-next part, right after writing the file from the ZIP, I try to delete the files.
At the moment there are two files in the directory, one ZIP and one PDF document from the ZIP.
The first file in itm is the PDF, it errors with an ACCESS DENIED.
The second file in itm is the ZIP which gets deleted.
The PDF is 311 kb in size.
Via Windows explorer I can delete it without any problem, even with the application still running.
Why is my file being locked?
What can I do to by-pass or remove this lock?
rg,
Eric
Found the problem.
The file that was added to the ZIP had it's attribute set to READ ONLY.
So when the application writes the file, windows would, as it should, not allow the application to delete it.
Unfortunately, windows does allow to delete the file via windows explorer without a message that the file is read only.
Have you also tried the File.Kill(fileLocation) method?
I am new to VB. Can anyone help on this, thanks. BTW, it's bloody pain to mess the formatting here :) How to do the indent?
parameters:
source file
target
backup folder
Pseudocode:
get all sub folders under target
for each folder under target
if exists source file
if exists backup folder
replace/copy source file to backup folder
else
create backup folder
replace/copy source file to backup folder
replace source file
Check this methods:
Directory.GetDirectories()
File.Exists()
Directory.Exists()
Directory.CreateDirectory()
File.Copy()
As I told you before on the other posts, just check the methods on System.IO. Its very well documented on MSDN, specially the classes File, Directory and Path.
For getting subfolders refer to this question
You then will want to put the sub folder locations into an array. From this array, loop through each object and so on.
Try using methods under 'File.' as this will include many functions for file processing.
This will require the 'Imports System.IO'.