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");
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"))
I searched for two hours, here on Stackoverflow and on other forums. But i can't find a solution for my problem. I have to be able to change the properties of an object that has a name that I don't know. Now i try to explain better:
The user drag some files in a form, and i get in a array() all the paths of the dragged files. For each path in files() i add to a panel a usercontrol that is the interface of an uploader. (My application it's kind of an uploader). Good, imagine that the user dragged 4 files, i have 4 different usercontrols, named "Uploader1", "Uploader2", "Uploader3" and "Uploader4". I need to change the text of a label in the uploader1, but i can't write:
Uploader1.LabelExample.Text = "Example"
Becouse it doesn't exist! (Not yet!)
So i tryed this method.
Dim UploadCounter as Integer = 1
Dim CurrentUploader = CType(Panel.Controls("Uploader" & UploaderCounter.ToString), UploadBanner)
CurrentUploader.LabelExample.Text = "Example"
I write the same with DirectCast and TryCast, but nothing.
I try also:
For Each Uploader With{.Name = "Uploader1"} as UploaderControl in Panel.Controls
Uploader.LabelExample.Text = "Example"
Next
I searched everywhere for "convert string to an object in vb.net" but i can't find anything that work! They all return "System.NullReferenceException: 'Object reference not set to an instance of an object.'"
Sorry for my bad bad english, thanks for all that will help me! need really!
The following data is uploaded to my GAE application -
How can I
get fields with files only
get filenames of the uploaded files?
get fields with files only
import cgi
values = self.request.POST.itervalues()
files = [v for v in values if isinstance(v, cgi.FieldStorage)]
get filenames of the uploaded files
filenames = [f.filename for f in files]
Edit: corrected snippet, now tested :)
Assuming the data is POSTed using a form, for #2, see Get original filename google app engine
For #1, you could iterate through the self.request.POST multidict and see anything that looks like a file. self.request.POST looks like this:
UnicodeMultiDict([(u'file_1', FieldStorage(u'file_1', u'filename_1')), (u'random_string_field', u'random_string_value')])
Hope that helps you out
-Sam
filename = self.request.POST['file'].filename
file_ext = self.request.POST['file'].type
OR
filename = self.request.params[<form element name with file>].filename
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.
I would like to get all the files that a sub-folder holds in a string array.
So, I have tried something like the following:
var IOstore = IsolatedStorageFile.GetUserStoreForApplication();
string searchpath = System.IO.Path.Combine("product", ProductName);
string filesInSubDirs[] = IOstore.GetFileNames(searchpath);
But I got all the files in the "product" folder. I have also tried with "productname" only as the parameter.
Thanks for your help.
The search pattern for a sub-folder needs to include "*.*" at the end to pattern match any file, which would make your code something like the following:
var IOstore = IsolatedStorageFile.GetUserStoreForApplication();
string searchpath = System.IO.Path.Combine("product", ProductName);
searchpath = string.Format("{0}\\*.*", searchpath);
string filesInSubDirs[] = IOstore.GetFileNames(searchpath);
Something you might want to try. (this is sort of a left field answer, sorry). In my dropbox client http://sharpdropbox.codeplex.com/) I have a set of facades for System.IO.File, System.IO.FileInfo, System.IO.Directory, and System.IO.DirectoryInfo. They work pretty good and I have tested them.
Basically, you add a Using or Import for System.IO.IsolatedStorage and then PSFile, PSDirectory, PSFileInfo, or PSDirectoryInfo. It's saved me from having to remember all the nuances... for instance if you are querying a directory, it knows to add a slash, etc. BTW, the "PS" prefix stands for "Persisted Storage" which is what IsolatedStorage is sometimes called (starting them with an "I" implies they are interfaces.. and having no prefix makes things even more confusing).
Anyway, you can grab the code from source or I believe the last release had the DLLs for them (it's called something like "IsolatedStorageFacade-WP7")