Using C#, I want to get the list of files in a folder.
My goal inside a combo box:
File1.txt
File2.txt
File3.txt
Process works when using Console.WriteLine however can't convert the string into the object, see below.
string[] files = Directory.GetFiles(dir);
foreach(string file in files)
ComboBox.Items.AddRange(Path.GetFileName(file));
Help would be appreciated! Thanks in advance!
If you want to stick with your foreach, you just need to change AddRange to Add:
foreach (string filePath in files) comboBox1.Items.Add(Path.GetFileName(filePath));
Here is another way using the LINQ extension method IEnumerable.Select like this:
comboBox1.Items.AddRange(files.Select((string filePath) => Path.GetFileName(filePath)).ToArray());
Related
I am using Blazor server. I have paintings in a wwwroot/paintings folder. The painting names are all in the format "XX by YY". I need to generate links to each painting using its name, so the link will also say "XX by YY." I can't know the names ahead of time.
How can I load the paintings into an array, or what is the best way to do this?
This is what you are looking for:
DirectoryInfo d = new DirectoryInfo(#"...\wwwroot\paintings\");
FileInfo[] files = d.GetFiles();
foreach (FileInfo filepath in files)
Console.WriteLine(filepath.Name);
last two lines just to print the names of all files you find. If needed you can specify file format in GetFiles() by putting for example "*.jpg" parameter
I have 4 combo boxes that are linked to a server folder that show all the .dotx available on the folder.
string[] files = Directory.GetFiles(#"location of the folder", "*.dotx");
foreach (string file in files)
comboBox1.Items.Add(Path.GetFileName(file));
foreach (string file in files)
comboBox2.Items.Add(Path.GetFileName(file));
foreach (string file in files)
comboBox3.Items.Add(Path.GetFileName(file));
foreach (string file in files)
comboBox4.Items.Add(Path.GetFileName(file));
I am using the code in this video "https://www.youtube.com/watch?v=0me-ntfD8Rk" with some minor changes.
I am a total newbie in c# and programming in general, I'm just trying to understand how I can make my directory path in my button (see video at 16:13) to be the directory chosen by the user in the combo box1 per example.
Any guidance would be really appreciated.
What I believe you're asking is how to reference the selected item in the combo box? If so you can simply do this:
comboBox1.Text
So in the video at 16:13 the call to the method would look like:
CreateWordDocument($"path\\of\\directory\\{comboBox1.Text}", #"path\to\output")
Where $"{variableName}" is a bit of shorthand that lets you reference a variable insde a string.
As a side note, you don't need to call the same loop four times like that. Instead it can look like:
foreach(string file in files)
{
string path = Path.GetFileName(file); // Note I can call GetFileName once and reuse the result
comboBox1.Items.Add(path);
comboBox2.Items.Add(path);
comboBox3.Items.Add(path);
comboBox4.Items.Add(path);
}
I'm developing a windows phone app. I saved many files on Isolated storage and i need shows them (file names) on ListBox.
My code works fine, but file names is show with extension (filename.doc). I want show file names without extension (filename).
My code:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string directory = "./preVenda/*.*";
string[] filenames = myIsolatedStorage.GetFileNames(directory);
List0.Items.Add(filenames);
I tried use "Remove" method, but not work.
You could use Path.GetFileNameWithoutExtension
foreach(string file in filenames)
List0.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
Also, you could try without an explicit loop (I suppose that list0 is a System.Windows.Control.ListBox)
list0.ItemsSource = filenames.Select(d => Path.GetFileNameWithoutExtension(d)).ToList();
(Warning. Not able to test this now)
I am trying to read all .txt files in a folder using stream reader. I have this now and it works fine for one file but I need to read all files in the folder. This is what I have so far. Any suggestions would be greatly appreciated.
using (var reader = new StreamReader(File.OpenRead(#"C:\ftp\inbox\test.txt")))
You can use Directory.EnumerateFiles() method instead of.
Returns an enumerable collection of file names that match a search
pattern in a specified path.
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
{
...
}
You can call Directory.EnumerateFiles() to find all files in a folder.
You can retrieve the files of a directory:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\");
Therefore you can iterate each file performing whatever you want. Ex: reading all lines.
And also you can use a file mask as a second argument for the GetFiles method.
Edit:
Inside this post you can see the difference between EnumerateFiles and GetFiles.
What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?
I am currently writing a program which searches My Documents. Currently my program is able to search and copy the main my documents folder but I am unable to make it search sub directory's within the main my documents directory. I have tried multiple methods but none seem to be working out.
Currently I am using the below code to dump the files location into an array called files. sourcePath is declared in an array before hand.
string[] files = System.IO.Directory.GetFiles(sourcePath[loopcounter]);
I then have a loop which copy's the files over to another directory
foreach (string s in files)
Any help as to how to fill the array files with details of files in the sub directories of a folder would be very handy. Thanks in advance!
Use research by pattern and specify you want use recursion :
var allFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"*",
SearchOption.AllDirectories);
foreach (var item in allFiles)
{
// Do Stuff...
}
If you want details about each file, then GetFiles returns you array of names. Pass each name to FileInfo API.