I am trying to load pictures that are in a certain folder (camera) into my application using a listview and pictureList. For some reason the files are loaded but do not appear in the listview.
This is the code I have so far:
try
{
listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(32, 32);
listView1.LargeImageList = imageList1;
DirectoryInfo directory = new DirectoryInfo(#"C:\");
FileInfo[] Archives = directory.GetFiles("*.JPG");
foreach (FileInfo fileinfo in Archives)
{
imageList1.Images.Add(Image.FromFile(fileinfo.FullName));
}
listView1.Update();
MessageBox.Show("I found " + imageList1.Images.Count.ToString() + " images!");
}
catch
{
MessageBox.Show("Something went wrong!");
}
Note that the messagebox is showing me the correct number of files, so I suppose I have some part right. Any clues what might be wrong?
Related
I want to make same Window File explorer.
but I don't know how to get file default Icon image.
If I can get file default image(Icon), I would like to add to the listview.
my code is as below
private void AddFiles(string strPath)
{
lv_local.BeginUpdate();
lv_local.Items.Clear();
iFiles = 0;
try
{
DirectoryInfo di = new DirectoryInfo(strPath + "\\");
FileInfo[] theFiles = di.GetFiles();
foreach (FileInfo theFile in theFiles)
{
iFiles++;
ListViewItem lvItem = new ListViewItem(theFile.Name);
lvItem.SubItems.Add(String.Format("{0:N0}", theFile.Length) + "KB");
lvItem.SubItems.Add(theFile.Extension);
lvItem.SubItems.Add(theFile.LastWriteTime.ToShortDateString());
lvItem.ImageIndex = 4;
// I want to put an image that was read default image
lv_local.Items.Add(lvItem);
}
}
catch (Exception Exc)
{
}
lv_local.EndUpdate();
}
Create ImageList and add icons
var imageList = new ImageList();
imageList.Images.Add("IconKey", icon);
Assign the ImageList to ListView
listView.LargeImageList = imageList;
Assign icon for the list view item
listViewItem.ImageKey = "itemImageKey";
or listViewItem.ImageIndex = 1;
I have a viewer.exe that loads at startup some models (*.mdl) from a "models" folder. Some of the models crash viewer.exe: "viewer.exe has stopped working. Windows can check online for a solution to the problem".
What I could do is move all the .mdl files in a "source" folder and then manually test for each .mdl file moved to "models" if viewer.exe is running but, there are a lot of files to check. How do I move each *.mdl file from "source" to "models" and test programmatically if viewer.exe is running correctly?
Here is the code I use for my first problem: to move the .mdl files from "source" folder sub-directories in "models". Some of the files had identical names but different size:
String mask = "*.mdl";
String source = #"c:\Source\";
String destination = #"c:\Models\";
String[] files = Directory.GetFiles(source, mask, SearchOption.AllDirectories);
foreach (String file in files)
{
if (File.Exists(file) && !File.Exists(destination + new FileInfo(file).Name))
{
File.Move(file, destination + new FileInfo(file).Name);
}
else
{
FileInfo f = new FileInfo(file);
long s = f.Length;
FileInfo f2 = new FileInfo(destination + new FileInfo(file).Name);
long s2 = f2.Length;
if (s >= s2)
{
File.Delete(destination + new FileInfo(file).Name);
File.Move(file, destination + new FileInfo(file).Name);
}
}
}
use process.start(startInfo) (see http://msdn.microsoft.com/en-gb/library/0w4h05yb.aspx)
Wait a few seconds, check if the process has terminated, then the returned process.hasexited (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx)
then kill it anyway using process.kill() (see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx)
You might need to turn off windows error reporting: http://msdn.microsoft.com/en-us/library/bb513638(VS.85).aspx
Surround the operations which can fail in try-catch statements
try {
File.Delete(destination + new FileInfo(file).Name);
} catch (Exception ex) {
// File could not be deleted
}
try {
File.Move(file, destination + new FileInfo(file).Name);
} catch (Exception ex) {
// File could not be moved
}
In the catch statement do whatever you want to do in case the files could not be processed.
I have disabled windows error reporting and this is how the program looks like now:
String mask = "*.mdl";
String source = #"c:\source\";
String destination = #"C:\Viewer\Models\";
String[] files = Directory.GetFiles(source, mask, SearchOption.AllDirectories);
foreach (String file in files)
{
if (File.Exists(file) && !File.Exists(destination + new FileInfo(file).Name))
{
File.Move(file, destination + new FileInfo(file).Name);
}
else
{
FileInfo f = new FileInfo(file);
long s = f.Length;
FileInfo f2 = new FileInfo(destination + new FileInfo(file).Name);
long s2 = f2.Length;
if (s >= s2)
{
File.Delete(destination + new FileInfo(file).Name);
File.Move(file, destination + new FileInfo(file).Name);
}
}
//mycompiledapp.exe is placed in Viewer folder for this to work
Process myprocess = Process.Start(#"viewer.exe");
Thread.Sleep(3000);
if (myprocess.HasExited) //Process crashes, exiting automatically
{
//Deletes the file that makes the viewer.exe crash
File.Delete(destination + new FileInfo(file).Name);
}
else
{
myprocess.Kill();
}
}
I have developed an application that loaded many images in a listview using ImageList in c# .net framework 4. The images are also compressed. When many many images are loaded and compressed then it takes a long time. So I call the method in backgroundworker. In the backgroundworker I had to add images to ImageList and add ImageList to ListView. So I have used safeinvoke() method listView1.SafeInvoke(d=>d.Items.Add(item)).
Everything works fine. Images are displayed one by one in the listview.
But the release of the application doesn’t work properly in some pc and properly works in some other pc. Doesn’t work properly means, If 100 images are browsed using OpenFileDialog to load then some images are loaded and added to listview and then the loading is automatically stopped without adding all images to the listview and no exception shows.
I have spent many times to solve this problem but couldn’t figure out the problem. . Where is the problem? Can anybody help me?
private void bgwLoading_DoWork(object sender, DoWorkEventArgs e)
{
ArrayList a = (ArrayList)e.Argument;
string[] fileNames = (string[])a[0];
this.loadMultiImages(fileNames);
}
private void loadMultiImages(string[] fileNames)
{
int i = 1;
int totalFiles = fileNames.Count();
foreach (string flName in fileNames)
{
if (!flName.Contains("Thumbs.db"))
{
Bitmap newBtmap = (Bitmap)Image.FromFile(flName);
FileInfo fi = new FileInfo(flName);
long l = fi.Length;
if (l > compressSize)
{
newBtmap = resizeImage(newBtmap, 1024,768) ;
newBtmap = saveJpeg(IMAGE_PATH + (SCANNING_NUMBER +
) + ".jpg", newBtmap, IMAGE_QUALITY);
}
else
{
File.Copy(flName, TEMP_IMAGE_PATH + (SCANNING_NUMBER + 1) + ".jpg");
}
if (!bgwLoading.CancellationPending)
{
CommonInformation.SCANNING_NUMBER++;
this.SafeInvoke(d => d.addItemToLvImageContainer(newBtmap));
bgwLoading.ReportProgress((int)Math.Round((double)i / (double)
(totalFiles) * 100));
i++;
}
}
}
}
}
public void addItemToLvImageContainer(Bitmap newBtmap)
{
imageList.Images.Add(newBtmap);
ListViewItem item;
item = new ListViewItem();
item.ImageIndex = SCANNING_NUMBER - 1;
item.Text = SCANNING_NUMBER.ToString();
lvImageContainer.Items.Add(item);
lvImageContainer.Items[item.ImageIndex].Focused = true;
}
To find out the error I have modified the code as follows:
I have commented the two lines
//newBtmap = resizeImage(newBtmap, 1024, 768);
// newBtmap = saveJpeg(IMAGE_PATH + scanning_number + ".jpg", newBtmap, Image_Quality );
and added try-catch as follows:
try
{
Bitmap newBtmap = (Bitmap)Image.FromFile(flName);
File.Copy(flName, CommonInformation.TEMP_IMAGE_PATH +
(CommonInformation.SCANNING_NUMBER + 1) + ".jpg");
if (!bgwLoading.CancellationPending)
{
this.SafeInvoke(d => d.imageList.Images.Add(newBtmap));
ListViewItem item;
item = new ListViewItem();
CommonInformation.SCANNING_NUMBER++;
item.ImageIndex = CommonInformation.SCANNING_NUMBER - 1;
item.Text = CommonInformation.SCANNING_NUMBER.ToString();
this.SafeInvoke(d => d.lvImageContainer.Items.Add(item));
bgwLoading.ReportProgress((int)Math.Round((double)i /
(double)(totalFiles) * 100));
this.safeInvoke(d=>d.addItemImageContainer(newBtmap))
catch (Exception ex)
{
MessageBox.Show( ex.Message);
}
It shows the error message after loading some images as "OutOfMemoryException"
Most probably the following line creates the exception:
Bitmap newBtmap = (Bitmap)Image.FromFile(flName);
But the image files are not corrupted and their file extension is .JPG.
How to get rid of this problem?
I have no answer, but i have some suggestions:
Check .NET framework version on computers with problems
Check, if you have permissions for files you trying to read
Use "try-catch" when you accessing files
And questions:
Is this project written in older version of .NET and migrated/upgraded to .NET 4.0?
Are you using any non-built-in assemblies or external dll's for image processing?
I am trying to retrieve all the files in all the folders I have in a directory .
But the result is quite random ..
I think the foreach is wrong ..
What I don't understand is why ?
Because in all the folders , we check all the files and then display a link buttons of all the files . But actually it's displaying a lot of folders , twice .
var DI = new DirectoryInfo("C://inetpub//wwwroot//ClientPortal//Files//")
.GetDirectories("*.*", System.IO.SearchOption.AllDirectories);
foreach (System.IO.DirectoryInfo D1 in DI)
{
System.IO.FileInfo[] fiArr = D1.GetFiles();
foreach (System.IO.FileInfo file in fiArr)
{
LinkButton lktest = new LinkButton();
lktest.Text = D1.Name;
form1.Controls.Add(lktest);
form1.Controls.Add(new LiteralControl("<br>"));
}
}
Can someone help me ?
Thanks a lot !
display a link buttons of all the files
Here you're creating link buttons with the name set to the directory when it sounds like you want the file instead (ie file.Name instead of D1.Name)
lktest.Text = D1.Name;
Does this help?
http://www.dreamincode.net/code/snippet1669.htm
public void GetDirStructure(string path)
{
try
{
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] subDirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
foreach(FileInfo fi in files)
{
Console.WriteLine(fi.FullName.ToString());
}
if (subDirs != null)
{
foreach (DirectoryInfo sd in subDirs)
{
GetDirStructure(path + #"\\" + sd.Name);
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
The first line of code seems like the culprit:
System.IO.DirectoryInfo[] DI = new System.IO.DirectoryInfo("C://inetpub//wwwroot//ClientPortal//Files//").GetDirectories("*.*", System.IO.SearchOption.AllDirectories);
Try using the following:
DirectoryInfo[] DI = new DirectoryInfo("C://inetpub//wwwroot//ClientPortal//File//").GetDirectories();
how open a folder from winforms in this way that user couldn't move from this folder to another?
He could only remove or add files to this folder.
And he could close this folder and return to his winforms app.
It's not really possible (I assume you mean open it in Explorer). Your best bet is to have him run as a user that only has permissions to open that folder. What I mean is that you should try and solve this using the inbuilt Windows ACL system.
Perhaps if you provide more details we can help solve the underlying issue.
path = folderBrowserDialog1.SelectedPath;
ImageList imageList1 = new ImageList();
imageList1.ImageSize = new Size(256, 256);
imageList1.ColorDepth = ColorDepth.Depth24Bit;
string[] iconFiles = Directory.GetFiles(path, "*.jpg");
foreach (string iconFile in iconFiles)
{
try
{
imageList1.Images.Add(Image.FromFile(iconFile));
}
catch
{
MessageBox("Error","");
}
}
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = imageList1;
for (int j = 0; j < imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
This displays thumbnails of files. How to stick this with files? Deleting, renaming??