Results 1 to 13 of 13

Thread: How to get the windows folder [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Resolved How to get the windows folder [Resolved]

    Hi, simple question, I just want to open a file stored in the windows folder, my problem is getting the windows folder location.
    I just need something like
    Dim path As String = Application.StartupPath
    but rather than pointing to the the application startup path I want the windows directory. which I belive can either be c:\windows (on XP) or c:\winnt

    Cheers.
    Last edited by Oliver1; Jun 19th, 2006 at 10:49 AM. Reason: Resolved

  2. #2
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: How to get the windows folder

    You need to add a reference to 'Scripting' to use a FileSystemObject to find the Windows Folder. It's done like this:

    1. From the Project menu, select Add Reference....
    2. In the COM-tab select Microsoft Scripting Runtime, click Select and click OK

    Now you can use the FileSystemObject like this:
    VB Code:
    1. Dim fso As New Scripting.FileSystemObject
    2. Dim sWinDir As String = fso.GetSpecialFolder(Scripting.SpecialFolderConst.WindowsFolder).Path
    There you go. It's harder than it should be. But this works.

    You can actually use this to delete the Windows folder, but I wouldn't recommend that.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How to get the windows folder

    Will this work?
    VB Code:
    1. MessageBox.Show(System.Environment.SystemDirectory)
    You can also use:
    VB Code:
    1. system.Environment.GetFolderPath
    To get pretty much any "common" directory that you might be after.

  4. #4
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: How to get the windows folder

    Quote Originally Posted by sevenhalo
    Will this work?
    VB Code:
    1. MessageBox.Show(System.Environment.SystemDirectory)
    That shows the System32 folder's location.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How to get the windows folder

    Quote Originally Posted by arsmakman
    That shows the System32 folder's location.
    With basic logic, you can determine the parent directory.

  6. #6
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: How to get the windows folder

    Okay, you win. But I still like mine better.

    Anyway, for the people of little IO-experience:
    VB Code:
    1. IO.Directory.GetParent(System.Environment.SystemDirectory).ToString()
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get the windows folder

    I'd say stick to .NET types and members if they're available before referencing COM libraries.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: How to get the windows folder

    Thanks, have gone for

    VB Code:
    1. Dim winfolder As String = System.Environment.SystemDirectory
    2.         Dim endpoint As Integer
    3.  
    4.         endpoint = InStr(winfolder, "System", CompareMethod.Text) - 1
    5.  
    6.  
    7.         winfolder = Mid(winfolder, 1, endpoint)
    8.         MsgBox(winfolder)

    Just waiting for someone to Incredible Hulk on me now for using old VB functions. I can never remember the correct .net functions.

  9. #9
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: How to get the windows folder

    I don't want to Incredible Hulk you, but I feel I have to, because I even gave the correct .net function away (see my post above)!
    And I didn't remember it, I found it. Because it's in a very logical place. It's really not that hard.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: How to get the windows folder [Resolved]

    Ah, but jmcilhinney who is never wrong (well I think I read one post where he was, but maybe I just dreamt it) said not to use a com libary if I could avoid it.

  11. #11
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: How to get the windows folder

    Quote Originally Posted by arsmakman
    Okay, you win. But I still like mine better.

    Anyway, for the people of little IO-experience:
    VB Code:
    1. IO.Directory.GetParent(System.Environment.SystemDirectory).ToString()
    I ment this ^ post.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: How to get the windows folder [Resolved]

    Argh, didn't realise your latest post didn't involve the com libary.
    Sorry have changed my code now, and please don't get all gamma radition like on me.

  13. #13
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: How to get the windows folder [Resolved]

    Quote Originally Posted by Oliver1
    please don't get all gamma radition like on me.
    I'll try...
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width