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.
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:
Dim fso As New Scripting.FileSystemObject
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. :)
Re: How to get the windows folder
Will this work?
VB Code:
MessageBox.Show(System.Environment.SystemDirectory)
You can also use:
VB Code:
system.Environment.GetFolderPath
To get pretty much any "common" directory that you might be after.
Re: How to get the windows folder
Quote:
Originally Posted by sevenhalo
Will this work?
VB Code:
MessageBox.Show(System.Environment.SystemDirectory)
That shows the System32 folder's location.
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.
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:
IO.Directory.GetParent(System.Environment.SystemDirectory).ToString()
Re: How to get the windows folder
I'd say stick to .NET types and members if they're available before referencing COM libraries.
Re: How to get the windows folder
Thanks, have gone for
VB Code:
Dim winfolder As String = System.Environment.SystemDirectory
Dim endpoint As Integer
endpoint = InStr(winfolder, "System", CompareMethod.Text) - 1
winfolder = Mid(winfolder, 1, endpoint)
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.
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.
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. :D
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:
IO.Directory.GetParent(System.Environment.SystemDirectory).ToString()
I ment this ^ post.
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.
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... ;)