0

I am using the following code in order to try to clear the Java cache:

Dim TheFolderPath As String = "C:\Users\%username%\AppData\LocalLow\Sun\Java\Deployment\cache"
'check if the folder exists  
If IO.Directory.Exists(TheFolderPath) Then
    'delete the folder and all its contents  
    My.Computer.FileSystem.DeleteDirectory(TheFolderPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
End If

However, when I run the tool to test its functionality the tool does not delete the folder. Can you please assist?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

2 Answers2

1

Your path string contains an environment variable (%username%) which you must expand first:

Dim TheFolderPath As String = "C:\Users\%username%\AppData\LocalLow\Sun\Java\Deployment\cache"
TheFolderPath = Environment.ExpandEnvironmentVariables(TheFolderPath)

%username% is a placeholder for the name of the currently logged-in user (Windows automatically creates an environment variable username with the username at login). However, inside a string the placeholder is just a literal string %username%. To actually have it substituted with the value of the environment variable you need to use the function mentioned above.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • @Chris06991 : He just gave you some code. Just put it in place of the one you have now. VB.NET doesn't read Environment variables such as `%username%`, so to get your path to work properly you must use the `Environment.ExpandEnvironmentVariables()` function. – Visual Vincent Mar 05 '16 at 11:07
1

You could also use the Environment.UserName property, which gets the logged in user's user name, and then construct a path with that and IO.Path.Combine().

Dim TheFolderPath As String = IO.Path.Combine("C:\Users", Environment.UserName, "AppData\LocalLow\Sun\Java\Deployment\cache")
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75