Blue Theme Orange Theme Green Theme Red Theme
 
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win $500 Cash
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » deleting a particular image from the folder which contains the images using c#
       
Author Reply
pinky rao
posted 2 posts
since Oct 19, 2009 
from

 deleting a particular image from the folder which contains the images using c#
  Posted on: 11/4/2009 1:23:04 AM       
hi, i have a query regarding deleting the images stored in a folder... my query is: how to store the images in the folder and displaying the image names in a listbox at pageload and deleting the selected image from a folder when we select the name in the listbox using c# plz help me out...its very urgent thanks and regards, pinky
krishna prasad
posted  105 posts
since  Dec 03, 2008 
from 

 Re: deleting a particular image from the folder which contains the images using c#
  Posted on: 11/4/2009 1:44:48 AM       
Hai..

here is the code for on button click it loads the list view item with the name of all photos in the specified folder..Don't forgot to add
using System.IO; in the begining .

 private void button1_Click(object sender, EventArgs e)
        {
            string[] fileEntries = Directory.GetFiles("E:\\Photos");
            foreach (string fileName in fileEntries)
            {
                // do something with fileName
                listBox1.Items.Add(fileName);
            }
        }


U can add in form load also..as u wish...and change the directory according to ur folder present..

If this wat u asked then reply i ll give the solution for delete files..


-----------------------------------------------------------------------------------------------
If someway my answer helped u don't forget to mark it as correct answer...
krishna prasad
posted  105 posts
since  Dec 03, 2008 
from 

 Re: deleting a particular image from the folder which contains the images using c#
  Posted on: 11/4/2009 1:56:10 AM   Accepted Answer    
For deleting the selected item in the listbox..Here is the code..


private void button2_Click(object sender, EventArgs e)
        {

            File.Delete(listBox1.SelectedItem.ToString());
            MessageBox.Show("file succesfully deleted");

        }


Here i have a created a button2 where if a select an item in the list and click this button2 the file gets deleted...

Hope this helps u..Fell free to ask any doubts....


-----------------------------------------------------------------------------------------------
If someway my answer helped u don't forget to mark it as correct answer...

niki d
posted  220 posts
since  Oct 27, 2006 
from 

 Re: deleting a particular image from the folder which contains the images using c#
  Posted on: 11/4/2009 2:03:19 AM       

Hi Pinky,
 
I tried my best to give you a descriptive answer. If you find it useful, please do accept my answer.
 
 

Let's say you added a form called Form1.
Then you dragged two buttons naemd button1(Open button) and button2(delete button).
You added events for the click events of those buttons as follows.
For button1 : button1_Click
For button2 : button2_Click

Then you added event for form load event.
Form1_Load

Then you dragged a listbox named listBox1.
Then you can use my code to do the rest.
 
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.IO;
namespace
WindowsFormsApplication3
{
public partial class Form1 : Form
{
string path = "C:\\images";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//Save images
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter =
"Image files (*.jpg)|*.jpg";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory =
true;
string sourceFile = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
sourceFile = openFileDialog1.FileName;
}
else
{
return;
}
string filename = Path.GetFileName(sourceFile);
//Let's say you want to add images to 'C:\images' folder.
string file = string.Format("{1}\\{0}", filename, path);
File.Copy(sourceFile, file);
listBox1.Items.Add(filename);
MessageBox.Show("Image saved");
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error! {0}", ex.Message));
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (listBox1.Items.Count == 0)
{
MessageBox.Show("No images");
return;
}
if (listBox1.SelectedIndex == -1)
{
MessageBox.Show("No Image selected");
return;
}
string filename = listBox1.SelectedItem.ToString();
string file = string.Format("{1}\\{0}", filename, path);
if (File.Exists(file))
{
File.Delete(file);
listBox1.Items.Remove(filename);
MessageBox.Show("Image deleted");
}
else
MessageBox.Show("File not found");
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error! {0}", ex.Message));
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
string filename = Path.GetFileName(file);
listBox1.Items.Add(filename);
}
}
catch (Exception ex)
{
}
}
}
}

 
Please accept my answer if it helps you.

Niki

Arumugam A
posted  4 posts
since  Nov 04, 2009 
from 

 Re: deleting a particular image from the folder which contains the images using c#
  Posted on: 11/4/2009 2:54:47 AM       
You can delete a file or a directory using the following code




  protected void lbtnremove1_Click(object sender, EventArgs e)
    {
        string imgname="Image1.jpg";
        try
        {
            File.Delete(Server.MapPath("../PropertyImages/12/" + imgname));
            File.Delete(Server.MapPath("../PropertyImages/12/"+ imgname));
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message.ToString();
        }

    }


U can either delete a root directory too by using the following code:


  protected void lbtnremove1_Click(object sender, EventArgs e)
    {
        string imgname="Image1.jpg";
        try
        {
            Directory.Delete(Server.MapPath("../PropertyImages/12));
            Directory.Delete(Server.MapPath("../PropertyImages/12));
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message.ToString();
        }

    }



Please accept this answer if u get satisfied


Cheers

Arumugam
       
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 3.2009.8.27
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved