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);
}
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
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());
I'm confronted with a design problem in my attempt to populate my datagridview with simple files.
I've declared a main directory in my Settings file. I need my datagridview to search through this parent directory in 7 subfolders. Each subfolder has a bunch of subfolders (names of machines I am managing). Each of those has contained in it the file I need to add to my grid.
Example:
C:\Users\me\Documents\MASTERDIRECTORY\Folder7\Machine Name1\file.txt
C:\Users\me\Documents\MASTERDIRECTORY\Folder7\Machine Name2\file.txt
Obviously some kind of recursive code is needed to perform the search, but how should I start? Performance wise, should I add these file paths to an array list and then translate that to my grid?
Something like this may help :-
string filePath = #"C:\Users\me\Documents\MASTERDIRECTORY\Folder7"
foreach (string Folder in Directory.GetDirectories(filePath))
{
foreach (string file in Directory.GetFiles(Folder))
{
// here you can grab the log file path and add it to you Gridview
}
}
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.
i have the files situated in the listview now i want to copy those files to a directory in windows..
how this action can be performed??
I don't know what the data in your ListView looks like, but in general you should use File.Copy:
string sourceFile = #"c:\sourcedir\file.ext";
File.Copy(sourceFile, Path.Combine(#"c:\targetdir", Path.GetFileName(sourceFile)));
Update
Here is an example of iterating of the the items in a ListView, copying files with names fetched from a sub item (this is assuming the file name is in the third column; change the index number to fetch the Sub Item containing the file name in your code):
foreach (ListViewItem item in listView1.Items)
{
string sourceFile = item.SubItems[2].Text;
File.Copy(sourceFile, Path.Combine(#"c:\targetdir", Path.GetFileName(sourceFile)));
}
you have a File.copy and File.create in system.io