System.Environment.GetFolderPath函数中未开放的枚举参数

System.Environment.GetFolderPath函数可以通过Environment.SpecialFolder枚举参数获取系统特定的目录,比如桌面,我的文档,收藏夹等..但是有部分参数.NetFrameWork2.0并未开放,比如AllUser用户的这些特殊目录。我们可以通过windowsApi函数SHGetFolderPath来获取,如果你能通过Reflector看一下FrameWork代码就明白了。 

代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;
namespace  ConsoleApplication1
{
    
class  Program
    
{
        [DllImport(
" shfolder.dll " , CharSet  =  CharSet.Auto)]
        
internal   static   extern   int  SHGetFolderPath(IntPtr hwndOwner,  int  nFolder, IntPtr hToken,  int  dwFlags, StringBuilder lpszPath);

        
static   void  Main( string [] args)
        
{
            
for  ( int  i  =   0 ; i  <=   255 ; i ++ )
            
{
                StringBuilder lpszPath 
=   new  StringBuilder( 260 );

                SHGetFolderPath(IntPtr.Zero, i, IntPtr.Zero, 
0 , lpszPath);

                Console.WriteLine(i.ToString() 
+   " : "   +  lpszPath);
            }

            Console.Read();
        }

    }

}

结果如下

0 :C:/Documents and Settings/Administrator/桌面
1 :
2 :C:/Documents and Settings/Administrator/「开始」菜单/程序
3 :
4 :
5 :C:/Documents and Settings/Administrator/My Documents
6 :C:/Documents and Settings/Administrator/Favorites
7 :C:/Documents and Settings/Administrator/「开始」菜单/程序/启动
8 :C:/Documents and Settings/Administrator/Recent
9 :C:/Documents and Settings/Administrator/SendTo
10 :
11 :C:/Documents and Settings/Administrator/「开始」菜单
12 :
13 :C:/Documents and Settings/Administrator/My Documents/My Music
14 :C:/Documents and Settings/Administrator/My Documents/My Videos
15 :
16 :C:/Documents and Settings/Administrator/桌面
17 :
18 :
19 :C:/Documents and Settings/Administrator/NetHood
20 :C:/WINDOWS/Fonts
21 :C:/Documents and Settings/Administrator/Templates
22 :C:/Documents and Settings/All Users/「开始」菜单
23 :C:/Documents and Settings/All Users/「开始」菜单/程序
24 :C:/Documents and Settings/All Users/「开始」菜单/程序/启动
25 :C:/Documents and Settings/All Users/桌面
26 :C:/Documents and Settings/Administrator/Application Data
27 :C:/Documents and Settings/Administrator/PrintHood
28 :C:/Documents and Settings/Administrator/Local Settings/Application Data
29 :
30 :
31 :C:/Documents and Settings/All Users/Favorites
32 :C:/Documents and Settings/Administrator/Local Settings/Temporary Internet Files
33 :C:/Documents and Settings/Administrator/Cookies
34 :C:/Documents and Settings/Administrator/Local Settings/History
35 :C:/Documents and Settings/All Users/Application Data
36 :C:/WINDOWS
37 :C:/WINDOWS/system32
38 :C:/Program Files
39 :C:/Documents and Settings/Administrator/My Documents/My Pictures
40 :C:/Documents and Settings/Administrator
41 :C:/WINDOWS/system32
42 :
43 :C:/Program Files/Common Files
44 :
45 :C:/Documents and Settings/All Users/Templates
46 :C:/Documents and Settings/All Users/Documents
47 :C:/Documents and Settings/All Users/「开始」菜单/程序/管理工具
48 :C:/Documents and Settings/Administrator/「开始」菜单/程序/管理工具
49 :
50 :
51 :
52 :
53 :C:/Documents and Settings/All Users/Documents/My Music
54 :
55 :C:/Documents and Settings/All Users/Documents/My Videos
56 :C:/WINDOWS/resources
57 :
58 :
59 :C:/Documents and Settings/Administrator/Local Settings/Application Data/Microsoft/CD Burning
 

具体意义可以参照Environment.SpecialFolder枚举说明和windows基本常识来理解。

这个错误通常是由于类型不匹配造成的。在这段代码中,String.Join方法需要一个字符串数组作为输入参数,而updatedLines是一个List(Of String)类型的对象。要解决这个错误,可以使用List(Of String)对象的ToArray方法将其转换为字符串数组,或者直接使用String类的Join方法来避免这个问题: 使用List(Of String)对象的ToArray方法: ``` Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click Dim comboBoxValue As String = ComboBox1.Text Dim filePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "abc.txt") If File.Exists(filePath) Then Dim existingText As String = File.ReadAllText(filePath) Dim lines() As String = existingText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) Dim updatedLines As New List(Of String)() For Each line As String In lines If Not line.StartsWith(comboBoxValue & ",") Then updatedLines.Add(line) End If Next File.WriteAllText(filePath, String.Join(Environment.NewLine, updatedLines.ToArray())) MessageBox.Show("已成功删除文本文件中对应的行。") Else MessageBox.Show("文本文件不存在。") End If End Sub ``` 直接使用String类的Join方法: ``` Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click Dim comboBoxValue As String = ComboBox1.Text Dim filePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "abc.txt") If File.Exists(filePath) Then Dim existingText As String = File.ReadAllText(filePath) Dim lines() As String = existingText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) Dim updatedLines As New List(Of String)() For Each line As String In lines If Not line.StartsWith(comboBoxValue & ",") Then updatedLines.Add(line) End If Next File.WriteAllText(filePath, String.Join(Environment.NewLine, updatedLines)) MessageBox.Show("已成功删除文本文件中对应的行。") Else MessageBox.Show("文本文件不存在。") End If End Sub ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值