Struggling with the basics of globalisation

  • Thread starter Thread starter Graeme Bell
  • Start date Start date
G

Graeme Bell

Hi all, my first post here.

I'm using VC++ 2005 Express. How do I get the names of special folders on
the local system?
For example, if I want to get the desktop folder name on any version of
windows I simply call

GetFolderPath(Environment::SpecialFolder::desktop);

I can get the array of folder names using

array<String ^> ^ special_folders = gcnew array<String ^>(100);
special_folders=
System::Enum::GetNames(System::Environment::SpecialFolder::typeid);

But that just gives me a list of names, which isn't very useful. Worse, the
order in which those strings are retrieved doesn't seem to be useful as an
index to call

Enum::GetName(Environment::SpecialFolder::typeid, index);

Some of them match, but others ... not useful.


I'm doing this so I can populate a TreeView with the localised names for
folders such as Desktop, My Computer, My Documents, etc. and do it
dynamically, at run-time.

I already posted in the internationalisation group, but that doesn't seem to
get much traffic, at least the English language one. :(


Thanks

Graeme P. Bell
 
I should have made clear that I'd like to be able to do this in clean .NET
code.

Graeme
 
Graeme Bell said:
Hi all, my first post here.

I'm using VC++ 2005 Express. How do I get the names of special folders on
the local system?
For example, if I want to get the desktop folder name on any version of
windows I simply call

GetFolderPath(Environment::SpecialFolder::desktop);

I can get the array of folder names using

array<String ^> ^ special_folders = gcnew array<String ^>(100);
special_folders=
System::Enum::GetNames(System::Environment::SpecialFolder::typeid);

But that just gives me a list of names, which isn't very useful. Worse,
the
order in which those strings are retrieved doesn't seem to be useful as an
index to call

Enum::GetName(Environment::SpecialFolder::typeid, index);

Some of them match, but others ... not useful.


There's probably a more efficient way to do it but you can use
System::Enum::Parse to convert your strings back into the actual values, and
then call GetFolderPath on that. Here is some ugly code that assumes a
listbox on a form:

for each (String ^s in special_folders)
{
listBox1->Items->Add( s + " " + System::Environment::GetFolderPath(
(System::Environment::SpecialFolder) (System::Enum::Parse(
System::Environment::SpecialFolder::typeid, s ) ) ) );
}

In fact, skip that entirely. Any time you have to convert to a string and
back you are probably doing something wrong. Instead of using
Enum::GetNames, use Enum::GetValues, and the code becomes much simpler.

Array ^a =
System::Enum::GetValues(System::Environment::SpecialFolder::typeid);

for each (System::Environment::SpecialFolder s in a)
{
listBox1->Items->Add( System::Environment::GetFolderPath( s ) );
}

Obviously you could still do something like

->Add( Enum::GetName( s ) + " " + System::Environment::GetFolderPath( s ) );
 
Back
Top