Windows Develop Bookmark and Share   
 index > Windows Forms General > How to make button to create new folder
 

How to make button to create new folder

Hello, how to add button that will create new folder base on current path?
  • Changed TypeHitman_b9 Tuesday, January 13, 2009 8:35 PM
  •  
Hitman_b9  Tuesday, January 13, 2009 8:33 PM

Call the method in a normal way :

private void btnNoviFolder_Click(object sender, EventArgs e)
{
//System.IO.Directory.CreateDirectory("Y:\\Iskratrade12"); kreira folder Iskratrade12
string path = Environment.CurrentDirectory + @"\dirtocreate";
Directory.CreateDirectory(path);
this.ListPogled(path);
}

  • Marked As Answer byHitman_b9 Thursday, January 15, 2009 2:07 PM
  •  
nikho  Thursday, January 15, 2009 11:25 AM
Add at begin of your file: using System.IO;


Put following in your button click event:

string path = Environment.CurrentDirectory + @"\dirtocreate";
Directory.CreateDirectory(path);



boothwine  Tuesday, January 13, 2009 8:42 PM
i have add what you wrote but nothing is happening, i was able to add directory with this code:
System.IO.Directory.CreateDirectory("Y:\\Iskratrade12");

But that will only create folder in specific path....
i can put you my code here if you want to see it, i have one parent and one child,, this button is in child form , where i have listview for browse specific disk/folder...
Hitman_b9  Tuesday, January 13, 2009 10:48 PM
Here is the code

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;
using System.Collections;



namespace ButtonScrool
{
public partial class frmMDIChild1 : Form
{
private System.Collections.Specialized.StringCollection folderCol;

public frmMDIChild1()
{
InitializeComponent();
//lista foldere i fajlove
folderCol = new System.Collections.Specialized.StringCollection();
napraviZaglavlja();
ListPogled(@"C:\");
folderCol.Add(@"C:\");
this.ListView1.ItemActivate += new System.EventHandler(this.ListView1_ItemActivate);

}

private void frmMDIChild1_Load(object sender, EventArgs e)
{

}


private void napraviZaglavlja()
{
ColumnHeader colHead;

//prvo zablavlje
colHead = new ColumnHeader();
colHead.Text = "Ime fajla";
this.ListView1.Columns.Add(colHead);//insert zaglavlje

//drugo zaglavlje
colHead = new ColumnHeader();
colHead.Text = "Velicina";
this.ListView1.Columns.Add(colHead);

// treco zalgavlje

colHead = new ColumnHeader();
colHead.Text = "Zadnja korekcija";
this.ListView1.Columns.Add(colHead);
}

private void ListPogled(string root)
{
try
{
//dve lokalne varijable za kreiranje itema koje se dodavaju na listu
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi;
//ako nema root foldera ne možemo dodati ništa

if (root.CompareTo("") == 0)
return;
//uzmi info o root-u

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);

//uzmi fajlove i foldere iz root-a
DirectoryInfo[] dirs = dir.GetDirectories();//folderi
FileInfo[] files = dir.GetFiles();//fajlovi

// Clear the ListView. Note that we call the Clear method on the
// Items collection rather than on the ListView itself.
// The Clear method of the ListView remove everything, including column
// headers, and we only want to remove the items from the view.

this.ListView1.Items.Clear();

this.lblPutanja.Text = root;




this.ListView1.BeginUpdate();
//trazi sve foldere na root-u
foreach (System.IO.DirectoryInfo di in dirs)
{
//napravi glavni list view
lvi = new ListViewItem();
lvi.Text = di.Name;//ime foldera
lvi.ImageIndex = 0;//ikona foldera je indeksa 0
lvi.Tag = di.FullName;//set fajla po putanji

//kreiraj 2 subitema

lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = "";// velicina--folder nema velicine pa je kolumna prazna

lvi.SubItems.Add(lvsi);//dodaj sub item na listview item
lvsi.Text = di.LastAccessTime.ToString();//zadnja ulazna kolumna
lvi.SubItems.Add(lvsi);//dodaj usb item na lisview item
//dodaj list view item to item collections od list view-a
this.ListView1.Items.Add(lvi);
}

//prodi kroz sve fajlove na root folderu

foreach (System.IO.FileInfo fi in files)
{
//kreiraj main list view item

lvi = new ListViewItem();
lvi.Text = fi.Name;//ime fajla
lvi.ImageIndex = 1;// ikona koju koristimo za prikatzivanje foldera ima index 1
lvi.Tag = fi.FullName;//set tag na putanju fajla

//kreiraj 2 sub itema

lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.Length.ToString(); //duzina fajla

lvi.SubItems.Add(lvsi);//dodavalje suitem kolekciji

lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.LastAccessTime.ToString();//zadnja kolumna ulaza

lvi.SubItems.Add(lvsi);

this.ListView1.Items.Add(lvi);


}
//otkljucaj list vie . insertani itemi se prikazuju
this.ListView1.EndUpdate();
}
catch (System.Exception err)
{
MessageBox.Show("error" + err.Message);
}
}


private void ListView1_ItemActivate(object sender, System.EventArgs e)
{
System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
string filename = lw.SelectedItems[0].Tag.ToString();

if (lw.SelectedItems[0].ImageIndex != 0)
{
try
{//pokusaj pokretanja fajla
System.Diagnostics.Process.Start(filename);
}
catch
{
//if pokusaj neuspijel samo izademo iz metode;
return;
}
}
else
{
//dodaj iteme
ListPogled(filename);
folderCol.Add(filename);
}
}

private void btnPovratak_Click(object sender, EventArgs e)
{
if (folderCol.Count > 1)
{
ListPogled(folderCol[folderCol.Count - 2].ToString());
folderCol.RemoveAt(folderCol.Count - 1);

}
else
{
ListPogled(folderCol[0].ToString());
}
}

private void radioVelikeIkone_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.ListView1.View = View.LargeIcon;
}

private void radioSmall_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.ListView1.View = View.SmallIcon;
}

private void radioLista_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.ListView1.View = View.List;
}

private void radioDetalji_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.ListView1.View = View.Details;
}

private void btnNoviFolder_Click(object sender, EventArgs e)
{
//System.IO.Directory.CreateDirectory("Y:\\Iskratrade12"); kreira folder Iskratrade12
string path = Environment.CurrentDirectory + @"\dirtocreate";
Directory.CreateDirectory(path);
}



}


}


Hitman_b9  Tuesday, January 13, 2009 10:50 PM
If I understand your code correctly you have some kind of file browser and ListPogled[ListPogled.Count-1] contains your current path?

Than creating a directory would become something like this:

string path = ListPogled[ListPogled.Count-1] + "\\DirToCreate";
Directory.CreateDirectory(path);
  • Marked As Answer byHitman_b9 Wednesday, January 14, 2009 11:41 AM
  • Unmarked As Answer byHitman_b9 Wednesday, January 14, 2009 11:42 AM
  •  
boothwine  Wednesday, January 14, 2009 8:52 AM
ListPogled is a method and it is not valid in given context
Hitman_b9  Wednesday, January 14, 2009 9:31 AM
You can try with Assembly.GetExecutingAssembly.Location instead of Environment.CurrentDirectory :

stringexe=Assembly.GetExecutingAssembly().Location;
stringexeDirectory=System.IO.Path.GetDirectoryName(exe);
System.IO.Directory.CreateDirectory(exeDirectory+"\\test");
nikho  Wednesday, January 14, 2009 10:32 AM
nothing :(
Hitman_b9  Wednesday, January 14, 2009 10:48 AM
Sorry, were I wrote ListPogled i meant folderCol. I mixed them up.

Does that help?
boothwine  Wednesday, January 14, 2009 10:49 AM
yes i figured out now... it adds new folder, but it cant be seen on once only when i click back and enter current location it shows.. if you know what i mean..
Hitman_b9  Wednesday, January 14, 2009 10:58 AM
i diint write it good for you to understand,, when i am on example: y:\fodler\(and in this path i add new folder) it is not showing. And when i reenter that path again (back and double klik tu enter same path) then it shows..
Hitman_b9  Wednesday, January 14, 2009 11:03 AM
You mean it doesn't show up in your listview?

Than you should refresh your listview (refill it) or just add the new directory to the listview. The listView it self doesn't know about its contents is reflecting the content of a directory...

At least if I understand you correctly...
boothwine  Wednesday, January 14, 2009 2:18 PM
it shows in list view, but not after i click the button, i can't see new fodler, and when i go one step back and i return to current position where i was adding new folder in first place it then shows the new fodler.
Hitman_b9  Thursday, January 15, 2009 6:56 AM
Probably because in this case, like boothwine suggested, your listview is filled again ( may be onItemActivate event ).
So after your click call yourListPogled method to refresh the list view
nikho  Thursday, January 15, 2009 9:37 AM
how to do that?
i was trying like this
listview1.refresh()......after buton click..
because listpogled is a method so i don't know how to write statment to do refresh
Hitman_b9  Thursday, January 15, 2009 11:08 AM

Call the method in a normal way :

private void btnNoviFolder_Click(object sender, EventArgs e)
{
//System.IO.Directory.CreateDirectory("Y:\\Iskratrade12"); kreira folder Iskratrade12
string path = Environment.CurrentDirectory + @"\dirtocreate";
Directory.CreateDirectory(path);
this.ListPogled(path);
}

  • Marked As Answer byHitman_b9 Thursday, January 15, 2009 2:07 PM
  •  
nikho  Thursday, January 15, 2009 11:25 AM
nice , finally it is working, and it enter the new folder after click. Nice..
Thank you guys.
One more question, what i have to call to write source code for right click button to enable copy and paste , since i am in fight with drag and drop.
Thank you again...
Hitman_b9  Thursday, January 15, 2009 11:41 AM
You could use a contextmenustrip, but you should start a new thread if you face problem to do that.
nikho  Thursday, January 15, 2009 12:52 PM
i will do some reading .. thank you for this.

Hitman_b9  Thursday, January 15, 2009 2:07 PM

You can use google to search for other answers

Custom Search

More Threads

• Multiple Forms
• MDI Children + Comboboxes datasource
• Using custom Event Arguments with a System.EventHandler
• Webbrowser Control used like windows Explorer - selected files
• minimizing and tiling windows
• Localizing windows form using Database
• I have a BIG problem with the form - problem with resizing
• Getting rid of the drop shadow in ToolStripMenuItems' DropDownCollection
• Redraw issue when scrolling under DataGridView's Virtual Mode
• Microsoft.AGL.Common.MISC.HandleAr() Exc