« Silverlight 5 (OOB) Win32 API GetLogicalDriveStrings
Hi,
Starting to use Silverlight 5 and enjoying P/Invoke!
We needed the ability to list drive letters on the user’s computer.
Using System.Runtime.InteropServices, we imported the GetLogicalDriveStrings method.
Then we created our own GetLogicalDrives() method which extracts the drive letters from the buffer that is returned.
Here’s the code:
using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System; using System.Collections.Generic; using System.Diagnostics; namespace SL5Oob { public partial class MainPage : UserControl { [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern uint GetLogicalDriveStrings(uint nBufferLength, [Out] char[] lpBuffer); public MainPage() { InitializeComponent(); } private void GetDrives_Click(object sender, RoutedEventArgs e) { GetLogicalDrives(); } private static void GetLogicalDrives() { const int size = 512; char[] buffer = new char[size]; uint code = GetLogicalDriveStrings(size, buffer); if (code == 0) { MessageBox.Show("Call failed"); return; } List<string> list = new List<string>(); int start = 0; for (int i = 0; i < code; ++i) { if (buffer[i] == 0) { string s = new string(buffer, start, i - start); list.Add(s); start = i + 1; } } foreach (string s in list) { Debug.WriteLine(s); } } } }
Enjoy,
JW.
0
-