I want to get a number of songs from a folder and list their names in a WPF Listview.
I also want each item in the list view to be a draggable file and can be copied from the list to the desktop. I've achieved this on one button, using the code:
Point mpos = e.GetPosition(null);
Vector diff = this.start - mpos;
string[] files = new String[1];
files[0] = #"C:\Song1.mp3";
DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, files),
DragDropEffects.Copy);
For that each item in the list needs to have a filepath string associated with it.
How do I:
1. Get the files from a folder and list them.
2. Associate with each one a filepath string for the dragging.
Thanks!
You can use Directory.GetFiles() to get all the file paths in a folder and then use Path.GetFileName() (or Path.GetFileNameWithoutExtension()) on each path returned to get just the file names.
Related
I'm trying to create an array with an assortment of different randomized image files in it to display on a set of buttons in Tkinter. When a given button is clicked I'd like to add the text of that file's name to a new array. Basically, when button with imageX is clicked add "imageX" to a new array.
Unfortunately, I always get a return that isn't the image's filename, or the variable that I've set to correspond to that image, but instead either:
"tkinter.PhotoImage object at X" (where is X is a location like "0x0000020FC894D2E0") if the command is populationbeta.append (population[0])
or
"pyimage#" (where # is an integer that seems to relate to the number of images in the source file), if I change the command to populationbeta.append (str(population[0]))
I feel like there should be a simple way of doing this and I've tried every work around I can think of but I'm not getting it to work. Any help would be very much appreciated! Thanks!
Here's a shortened/simplified version of the code in question:
master=tkinter.Tk()
master.title("Not working")
a1b1c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c1.png")
a1b1c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c2.png")
a1b1c3 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c3.png")
a1b2c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c1.png")
a1b2c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c2.png")
population = [a1b1c1, a1b1c2, a1b1c3, a1b2c1, a1b2c2]
populationbeta = []
populationbeta.append(population[0])
print(populationbeta)
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
Correct. That shows that you have a list of PhotoImage objects. If you want the filenames you can use .cget('file') on the objects. cget is a common tkinter method for getting the value of a configured option.
filenames = [image.cget('filename') for image in population]
Or, if you don't want to use a list comprehension to create a list of filenames, you can do it on an individual image like so:
populationbeta.append(population[0].cget("file"))
Good day all, I have read through all the posts I could find, but none helped me.
I have a listbox that should list all the files in a folder called data that is in the same location as my app.
Problem is, I tried varies codes but i'm still failing to get the folder to show in my listbox.
The filepath is variable as the exe file is in diff locations on diff pc's.
Here is my code:
string Cust = System.AppDomain.CurrentDomain.BaseDirectory + #"data\";
string[] txtfiles = Directory.GetFiles(Cust, "*.txt");
foreach (string file in txtfiles)
custList.Items.Add(file);
When I finally get the files to list, I will need to be able to click on one and have its values display in labels on my form.
Any help would be great.
Thanx
Your code should work, your problem is most likely how you are concatenating your path:
Change:
string Cust = System.AppDomain.CurrentDomain.BaseDirectory + #"data\";
To:
string Cust = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "data");
I need to search an expression inside a xps document then list all matches (with the page number of each match).
I searched in google, but no reference or sample found which addresses this issue .
SO: How can I search a xps document and get this information?
The first thing to note is that an XPS file is an Open Packaging package. It can be opened and the contents accessed via the System.IO.Packaging.Package class. This makes any operations on the contents much easier.
Here's an example of how to search the page content with a given regex, while also tracking which page the match occurs on.
var regex = new Regex(#"th\w+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
using(var xps = System.IO.Packaging.Package.Open(#"C:\path\to\regex.oxps"))
{
var pages = xps.GetParts()
.Where (p => p.ContentType == "application/vnd.ms-package.xps-fixedpage+xml")
.ToList();
for (var i = 0; i < pages.Count; i++)
{
var page = pages[i];
using(var reader = new StreamReader(page.GetStream()))
{
var s = reader.ReadToEnd();
var matches = regex.Matches(s);
if (matches.Count > 0)
{
var matchText = matches
.Cast<Match>()
.Aggregate (new StringBuilder(), (agg, m) => agg.AppendFormat("{0} ", m.Value));
Console.WriteLine("Found matches on page {0}: {1}", i + 1, matchText);
}
}
}
}
It is not going to be as simple as you might have thought. XPS files are compressed (zipped) files containing a somewhat complex folder structure containing all the text, fonts, graphics and other items. You can use compression tools such as 7-Zip or WinZip etc. to extract the entire folder structure from an XPS file.
Having said that, you can use the following sequence of steps to do what you want:
Extract the contents of your XPS file programmatically in a temp folder. You can use the new ZipFile class for this purpose if you're using .NET 4.5 or better.
The extracted folder will have the following folder structure:
_rels
Documents
1
_rels
MetaData
Pages
_rels
Resources
Fonts
MetaData
Go to Documents\1\Pages\ subfolder. Here you'll find one or more .fpage files, one for each page of your document. These files are in XML format and contain all text contained in the page in a structured manner.
Use simple loop to iterate through all .fpage files, opening each of them using an XML reader such as XDocument or XmlDocument and search for required text in node values using RegEx.IsMatch(). If found, note down the page number in a List and move ahead.
I want to search a folder by its name. But I don't know the location of the folder.
Have to get the path of that specific folder.
How Can i do it?
You have to specify the directory to search for the folder using Directory.GetDirectories Method (String, String, SearchOption)
string[] directories = Directory.GetDirectories(#"c:\",
"*",
SearchOption.AllDirectories);
To get all drives from the computer, use DircotoryInfo.GetDrives and then search in all of them you can try:
DriveInfo[] allDrives = DriveInfo.GetDrives();
List<string> directoryList = new List<string>();
foreach (DriveInfo d in allDrives)
{
directoryList.AddRange(Directory.GetDirectories(d.Name , "*", SearchOption.AllDirectories));
}
// Only get subdirectories that begin with the letter "p."
string[] dirs = Directory.GetDirectories(#"c:\", "p*");
Console.WriteLine("The number of directories starting with p is {0}.",dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
Reference - Directory.GetDirectories Method (String, String)
If you dont know the drive then you need to search for all drives by changing the drives available on your system.
the only solution is using recursive search to surf all available folders and sub folders and also to jump access denied paths to have complete list of target result.
I have created a "FileTree" class which displays a file system via a JTree, and only displays folders and ".xlsx" or ".xls" files. I want to be able to add a double-click action to the DefaultMutableTreeNodes if they are a ".xlsx" or ".xls" file. It doesnt seem possible to be able to add an ActionListener or a MouseListener to a DefaultMutableTreeNode, is there a way I can control the double-click action on these nodes?
I have a solution of sorts, its not the one I was looking for, but it does work.
I have added a MouseListener to the JTree, and then when the click count is 2, I check the event source is an instanceof the JTree, then call
Object comp = tree.getLastSelectedPathComponent();
if (comp instanceof FileTreeNode)
{
FileTreeNode ftn = (FileTreeNode) comp;
File file = ftn.getFile();
}
and then I can do whatever I want with the file. FileTreeNode is an extension of DefaultMutableTreeNode which contains the file at that node.