Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code here:
C#
private void Button2_Click(object sender, EventArgs e)
       {

               string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Templates));
               foreach (string filePath in filePaths)
               {
                   listBox1.Items.Add(filePath);
               }


       }

but when button2 clicked, no item was added in listbox1(there are many files in temp folder).
help me!

What I have tried:

I have tried FileInfo but it failed
Posted
Updated 8-Jul-19 22:03pm
Comments
phil.o 9-Jul-19 3:17am    
Chances are, 'temp' and 'templates' folders are two distinct folders. Why don't you debug and find out?

Quote:
ut when button2 clicked, no item was added in listbox1(there are many files in temp folder).
There could be many files there, but that's irrelevant, since you asked for
Environment.SpecialFolder.Templates
that is "The directory that serves as a common repository for document templates" (see the documentation[^]).
 
Share this answer
 
We can't help you - we don't have any access to your file system.
But you do: so, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. At a guess, you are getting no files returned, so you need to first look at what Environment.GetFolderPath(Environment.SpecialFolder.Templates) is returning and look at the folder itself to start working out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900