newby - Help with FILE.DELETE

D

DavidB

I'm trying to delete a file with

File.Delete("c:\Documents and Settings\%username%\Application
Data\Microsoft\Excel\excel*.xlb")

On doing this I get an Exception "Illegal characters in path" - I assume its
the %username% bit

I trierd

Dim UserName As String %username%
File.Delete("c:\Documents and Settings\" & UserName & "\Application
Data\Microsoft\Excel\excel*.xlb")

Still get the same error

How can I get the Username from the system !!???
 
J

José Joye

To get an environment variable, use Environment.GetEnvironmentVariable()
method

To get access to the special folder, you can also use the
Environment.GetFolderPath() Method

- José
 
H

Herfried K. Wagner [MVP]

David,

DavidB said:
I'm trying to delete a file with

File.Delete("c:\Documents and Settings\%username%\Application
Data\Microsoft\Excel\excel*.xlb")

On doing this I get an Exception "Illegal characters in path" - I assume
its
the %username% bit

I trierd

Dim UserName As String %username%
File.Delete("c:\Documents and Settings\" & UserName & "\Application
Data\Microsoft\Excel\excel*.xlb")

AFAIK 'File.Delete' does not support wildcards ("*", in this particular
case). The code below is untested, but it should give you an idea on how to
solve the problem:

\\\
Imports System
Imports System.IO
..
..
..
Dim FileNames() As String = _
Directory.GetFiles( _
Path.Combine( _
Environment.GetFolderPath( _
Environment.SpecialFolder.LocalApplicationData _
), _
"Microsoft\Excel\excel*.xlb" _
) _
)
For Each FileName As String In FileNames
File.Delete(FileName)
Next FileName
///
 
H

Herfried K. Wagner [MVP]

Supra said:
% is integer is valid for vb.net
can u remove %?

I assume the OP wanted to type 'Dim UserName As String = '"%username%"'. So
there is no integer...
 
H

Herfried K. Wagner [MVP]

José Joye said:
To get an environment variable, use Environment.GetEnvironmentVariable()
method

ACK. Alternatively you can use 'Environment.ExpandEnvironmentVariables' to
expand all environment variables which are contained in a certain string.
 
K

Ken Tucker [MVP]

Hi,

Dim UserName As String = Environment.UserName

Ken
---------------------
I'm trying to delete a file with

File.Delete("c:\Documents and Settings\%username%\Application
Data\Microsoft\Excel\excel*.xlb")

On doing this I get an Exception "Illegal characters in path" - I assume its
the %username% bit

I trierd

Dim UserName As String %username%
File.Delete("c:\Documents and Settings\" & UserName & "\Application
Data\Microsoft\Excel\excel*.xlb")

Still get the same error

How can I get the Username from the system !!???
 
C

Cor Ligthert

DavidB,

I am in doubt about your question is it FILE.DELETE or how to concatinate a
string, or how to get the username best way to get the applicationData
folder from the current user.

The first answer is,
In the way you do that.

The second answer is (see the next answer)
" & myUsername & "

The third answer is
dim myUsername = environment.username

The fourth answer is:
dim mypath as string =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

I hope this helps?

Cor
 
J

José Joye

Good to know :)

- José
Herfried K. Wagner said:
ACK. Alternatively you can use 'Environment.ExpandEnvironmentVariables'
to expand all environment variables which are contained in a certain
string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top