我已经制作了一些代码来搜索目录并在列表框中显示文件.
DirectoryInfo dinfo2 = new DirectoryInfo(@"C:\Users\Hunter\Downloads");
FileInfo[] Files2 = dinfo2.GetFiles("*.sto");
foreach (FileInfo file2 in Files2)
{
listBox1.Items.Add(file2.Name);
}
Run Code Online (Sandbox Code Playgroud)
我甚至试过这个:
string path = Environment.SpecialFolder.UserProfile + @"\Downloads";
DirectoryInfo dinfo2 = new DirectoryInfo(Environment.SpecialFolder.UserProfile + path);
FileInfo[] Files2 = dinfo2.GetFiles("*.sto");
foreach (FileInfo file2 in Files2)
{
listBox1.Items.Add(file2.Name);
}
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误...
好吧,它说的Users\Hunter好吧,当人们拿到我的软件时,这个名字并不是猎人......所以我怎么把它带到任何用户下载文件夹的位置?
在我的机器上,它在这里:
string downloadsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Downloads");
Run Code Online (Sandbox Code Playgroud)
但在同事机器上,此文件夹不存在,他的"下载"文件夹位于"我的文档"文件夹中.我们都在Windows 7*上.
*编辑:事实上,事实证明他没有在自己的机器上运行应用程序,而是Windows Server 2003机器.
好的,我有一个OpenFileDialog,我想将初始目录设置为用户的'Download'文件夹.这是一个内部应用程序,因此,我确信用户将使用Windows 7.
var ofd = new OpenFileDialog();
//This doesn't work
ofd.InitialDirectory =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Downloads");
//This doesn't work either
ofd.InitialDirectory = @"%USERPROFILE%\Downloads";
ofd.Filter = "Zip Files|*.zip";
ofd.ShowDialog();
txtFooBar.Text = ofd.FileName;
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经尝试了上述内容,但都没有工作 没有抛出异常,它只是没有将初始目录设置为downloads文件夹.
我哪里错了?
谢谢