newby - Help with FILE.DELETE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DavidB

    newby - Help with FILE.DELETE

    I'm trying to delete a file with

    File.Delete("c: \Documents and Settings\%usern ame%\Applicatio n
    Data\Microsoft\ Excel\excel*.xl b")

    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 & "\Applicati on
    Data\Microsoft\ Excel\excel*.xl b")

    Still get the same error

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


  • Supra

    #2
    Re: newby - Help with FILE.DELETE

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


    DavidB wrote:
    [color=blue]
    >I'm trying to delete a file with
    >
    >File.Delete("c :\Documents and Settings\%usern ame%\Applicatio n
    >Data\Microsoft \Excel\excel*.x lb")
    >
    >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 & "\Applicati on
    >Data\Microsoft \Excel\excel*.x lb")
    >
    >Still get the same error
    >
    >How can I get the Username from the system !!???
    >
    >
    >
    >[/color]

    Comment

    • José Joye

      #3
      Re: newby - Help with FILE.DELETE

      To get an environment variable, use Environment.Get EnvironmentVari able()
      method

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

      - José
      "Supra" <supra56@rogers .com> a écrit dans le message de news:
      %23QwzUON8EHA.3 760@TK2MSFTNGP1 0.phx.gbl...[color=blue]
      >% is integer is valid for vb.net
      > can u remove %?
      >
      >
      > DavidB wrote:
      >[color=green]
      >>I'm trying to delete a file with
      >>
      >>File.Delete(" c:\Documents and Settings\%usern ame%\Applicatio n
      >>Data\Microsof t\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 & "\Applicati on
      >>Data\Microsof t\Excel\excel*. xlb")
      >>
      >>Still get the same error
      >>
      >>How can I get the Username from the system !!???
      >>
      >>
      >>[/color]
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: newby - Help with FILE.DELETE

        David,

        "DavidB" <davidb@notreal ly_hotmail.com> schrieb:[color=blue]
        > I'm trying to delete a file with
        >
        > File.Delete("c: \Documents and Settings\%usern ame%\Applicatio n
        > Data\Microsoft\ Excel\excel*.xl b")
        >
        > 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 & "\Applicati on
        > Data\Microsoft\ Excel\excel*.xl b")[/color]

        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.GetFi les( _
        Path.Combine( _
        Environment.Get FolderPath( _
        Environment.Spe cialFolder.Loca lApplicationDat a _
        ), _
        "Microsoft\Exce l\excel*.xlb" _
        ) _
        )
        For Each FileName As String In FileNames
        File.Delete(Fil eName)
        Next FileName
        ///

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: newby - Help with FILE.DELETE

          "Supra" <supra56@rogers .com> schrieb:[color=blue]
          >% is integer is valid for vb.net
          > can u remove %?[/color]

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

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: newby - Help with FILE.DELETE

            "José Joye" <jose.joye__KIL LTHESPAMS__SMAP SEHTLLIK__@blue win.ch> schrieb:[color=blue]
            > To get an environment variable, use Environment.Get EnvironmentVari able()
            > method[/color]

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

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


            Comment

            • Ken Tucker [MVP]

              #7
              Re: newby - Help with FILE.DELETE

              Hi,

              Dim UserName As String = Environment.Use rName

              Ken
              ---------------------
              "DavidB" <davidb@notreal ly_hotmail.com> wrote in message
              news:ufPxeMN8EH A.1596@tk2msftn gp13.phx.gbl...
              I'm trying to delete a file with

              File.Delete("c: \Documents and Settings\%usern ame%\Applicatio n
              Data\Microsoft\ Excel\excel*.xl b")

              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 & "\Applicati on
              Data\Microsoft\ Excel\excel*.xl b")

              Still get the same error

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



              Comment

              • Cor Ligthert

                #8
                Re: newby - Help with FILE.DELETE

                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.use rname

                The fourth answer is:
                dim mypath as string =
                Environment.Get FolderPath(Envi ronment.Special Folder.Applicat ionData)

                I hope this helps?

                Cor


                Comment

                • José Joye

                  #9
                  Re: newby - Help with FILE.DELETE

                  Good to know :-)

                  - José
                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> a écrit dans le
                  message de news: e5UivbN8EHA.323 6@TK2MSFTNGP15. phx.gbl...[color=blue]
                  > "José Joye" <jose.joye__KIL LTHESPAMS__SMAP SEHTLLIK__@blue win.ch> schrieb:[color=green]
                  >> To get an environment variable, use Environment.Get EnvironmentVari able()
                  >> method[/color]
                  >
                  > ACK. Alternatively you can use 'Environment.Ex pandEnvironment Variables'
                  > to expand all environment variables which are contained in a certain
                  > string.
                  >
                  > --
                  > M S Herfried K. Wagner
                  > M V P <URL:http://dotnet.mvps.org/>
                  > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                  >[/color]


                  Comment

                  Working...