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.
|
|
|
|
|
|
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
|
|
|
|
|
|