Im trying to load images from a folder and then inserting them into a tag.
For now i have this that i took from another question:
public string GetImage()
{
string imPath;
imPath = HttpContext.Current.Server.MapPath("~/Layout/Images/Banner");
DirectoryInfo directoryInfo = new DirectoryInfo(imPath);
FileInfo[] fileInfo = directoryInfo.GetFiles();
ArrayList arrayList = new ArrayList();
foreach (FileInfo fi in fileInfo)
arrayList.Add(fi.FullName);
return imPath;
}
But its not returning the images, only the folder path.
Just adding an answer so I can point out what everyone is confused about here.
What you've done here:
public string GetImage()
{
string imPath;
imPath = HttpContext.Current.Server.MapPath("~/Layout/Images/Banner");
DirectoryInfo directoryInfo = new DirectoryInfo(imPath);
FileInfo[] fileInfo = directoryInfo.GetFiles();
ArrayList arrayList = new ArrayList();
foreach (FileInfo fi in fileInfo)
arrayList.Add(fi.FullName);
return imPath;
}
Is equivalent to the following:
public string GetImage()
{
return HttpContext.Current.Server.MapPath("~/Layout/Images/Banner");
}
This is why everyone is a little confused.
Perhaps this is what you want (refactored a little bit)?
public ArrayList GetImage()
{
DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Layout/Images/Banner"));
ArrayList arrayList = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles())
arrayList.Add(fi.FullName);
return arrayList;
}
It's because you return the image path.. You probably want the ArrayList you're making.
change:
return imPath;
to:
return arrayList;
and change:
public string GetImage()
to:
public ArrayList GetImage()
I also reccomend renaming that arraylist, and try to do a little more research before asking questions. If you understood what return is, you would've know what the problem is.
And as you can read in the comments of your question, you shouldn't be using ArrayList. I'm no expert on C# so that will have to be answered by someone else.
Related
private void getTotalBytes(IEnumerable<string> urls)
{
files = new List<FileInfo>(urls.Count());
For example urls count is 399.
But when I'm using a break point and hit F11, I see that the count of files is 0.
With new List<FileInfo>(urls.Count()); you create an empty list with an expected capacity of 399. Therefore the count of files is 0.
The next step is to fill the list with actual FileInfo objects; e.g.
private void getTotalBytes(IEnumerable<string> urls)
{
files = new List<FileInfo>(urls.Count());
foreach (var url in urls)
{
files.Add(new FileInfo(url));
}
or with Linq
private void getTotalBytes(IEnumerable<string> urls)
{
files = urls.Select(u => new FileInfo(u)).ToList();
To literally answer your question:
var files = new List<FileInfo>();
foreach(var file in myEnumerableStringCollection)
{
files.Add(new FileInfo(file));
}
However, your example's variable is 'urls' so I suspect you're attempting to do more with this than you explained.
I have a .NET webform that is displaying files found in a directory in a listView. This is the code for the display:
private void files()
{
try
{
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
listView1.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
label2.Text contains the directory that houses the files. What I need is for a second listView to display a list of documents housed in another directory to display if the file does not appear in the first list view.
The second directory contains templates where as the first directory contains completed documents. The names are different in each directory, but they are similar. For example a completed document displayed in the first listView may be called DEFECT1_AA09890.doc. It's template may be called 05DEFECT.doc.
It is easy enough to display the contents of the template directory using this code:
private void templateDocuments()
{
string path = #"\\directoryname\foldername";
try
{
DirectoryInfo dinfo = new DirectoryInfo(path);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
listView2.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
But this does not compare contents and display based on the results.
Long story short, I want to display the contents of a directory in a listView, compare it to the contents of another directory, and display in a second listView what does not appear in the first.
Any help would be much appreciated.
Cheers.
Before adding file names to listView2, you need to check whether you already added them to listView1. One way of doing that is to store the files in listView1 in a HashSet<string>, then checking that before adding to listView2. Something like this should work:
private void filesAndTemplates()
{
string path = #"\\directoryname\foldername";
HashSet<string> files = new HashSet<string>();
try
{
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
files.Add(file.Name);
listView1.Items.Add(file.Name);
}
dinfo = new DirectoryInfo(path);
Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
if (files.Contains(file.Name))
{
continue; // We already saw this file
}
listView2.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
EDIT
If you want inexact matching, you need to reduce the file name to its essence -- remove any decorations, which in your case looks to be one (or both) of
Leading digits
Underscore followed by whatever
The essence of 01hello_world.doc would thus be hello.
Regex should fit the bill quite nicely -- although the exact definition of the regular expression would depend on your exact requirements.
Define the Regex and a transformation method somewhere suitable:
private static readonly Regex regex = new Regex(
#"[0-9]*(?<core>[^_]+)(_{1}.*)?", RegexOptions.Compiled);
private static string Transform(string fileName)
{
int extension = fileName.LastIndexOf('.');
if (extension >= 0)
{
fileName = fileName.Substring(0, extension);
}
Match match = regex.Match(fileName);
if (match.Success)
{
return match.Groups["core"].Value;
}
return fileName;
}
Then modify the original method to transform the filename before adding files to the HashSet and before checking for their presence:
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
files.Add(Transform(file.Name)); // Here!
listView1.Items.Add(file.Name);
}
dinfo = new DirectoryInfo(path);
Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
if (files.Contains(Transform(file.Name))) // Here!
{
continue;
}
listView2.Items.Add(file.Name);
}
Note the two calls to Transform.
This question already has answers here:
Getting file names without extensions
(14 answers)
Closed 6 years ago.
I'm currently creating a note taking application. I'm trying to add items to a listbox without their file extensions, I have tried GetAllFilesWithoutExtensions, but no luck. I'm currently able to add them but can't seem to remove the extension. Any advice would be greatly appreciated...
DirectoryInfo dir = new DirectoryInfo("../Debug/");
FileInfo[] files = dir.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file);
}
You can use LINQ and System.IO.Path.GetFileNameWithoutExtension
var fileNamesWithoutExtension = files
.Select(fi => System.IO.Path.GetFileNameWithoutExtension(fi.Name));
foreach(string fn in fileNamesWithoutExtension)
{
// ...
}
You could use System.IO.Path.GetFileNameWithoutExtension(string)
See MSDN
Create a class to help you:
class FInfo
{
public FileInfo Info { get; set; }
public FInfo (FileInfo fi) { Info = fi; }
public override string ToString()
{
return Path.GetFileNameWithoutExtension(Info.FullName);
}
}
Now fill the ListBox with instances of that class:
DirectoryInfo dir = new DirectoryInfo(yourPath);
FileInfo[] files = dir.GetFiles(yourFilter);
foreach (FileInfo file in files) listBox1.Items.Add(new FInfo(file));
Now you can access the full FileInfo data:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FInfo fi = listBox1.SelectedItem as FInfo;
if (fi != null) Console.WriteLine(fi.Info.FullName); // do your thing here!
}
And the ListBox will use the ToString method to display only the file names without path or extension..
How can I load all Images into an image list from a specific folder in c#? thanks!
List<Texture2D> images = new List<Texture2D>();
string folderPath = "MyImages/";
for(int i= 0; i<Count; i++) {
try{
images.Add(Content.Load<Texture2D>(folderPath + i.ToString));}
catch{break;}
}
This works but I need to convert the filenames into 1 to N. But I have to keep the filenames(the name of the personImage) for future use (for recognition output).
If you mean the image paths, you could try like this:
public List<String> GetAllImages(String directory)
{
return Directory.GetFiles(directory, "*.jpg", SearchOption.AllDirectories).ToList();
}
You can use foreach, in this case: (Don't forget to add using System.IO)
Here, set the folder to the DirectoryInfo
DirectoryInfo directory = new DirectoryInfo(#"C:\");
To get the right files you have to specify the type, i'm using jpg but can be png, bmp etc.
FileInfo[] Archives = directory.GetFiles("*.jpg");
Now, for each archive in the folder it will add to imagelist
foreach (FileInfo fileinfo in Archives)
{
imageList.Images.Add(Image.FromFile(fileinfo.FullName));
}
I've tested, worked. Alright?
I am trying to add items from FileInfo into my RadListBox although I am not able to, I tried casting the file into a RadListBoxItem object, but I get the error that it can not convert a string to a radlistboxitem. Can someone shed a little light? thanks.
DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(fp));
lb_Files.Items.Clear();
foreach (FileInfo file in dir.GetFiles())
{
RadListBoxItem rlb = new RadListBoxItem();
rlb = (RadListBoxItem)file.ToString();
//radListBox
lb_Files.Items.Add(rlb.ToString());
}
Try this
DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(fp));
lb_Files.Items.Clear();
foreach (FileInfo file in dir.GetFiles())
{
lb_Files.Items.Add(new RadListBoxItem(file.ToString(), file.ToString()));
}
No you cannot cast a String object into a RadListBoxItem, you must create a RadListBoxItem using that string as your Value and Text properties:
So replace this:
RadListBoxItem rlb = new RadListBoxItem();
rlb = (RadListBoxItem)file.ToString();
//radListBox
lb_Files.Items.Add(rlb.ToString());
With this:
lb_Files.Items.Add(new RadListBoxItem
{
Value = file.ToString(),
Text = file.ToString()
});