opening files
i have created a small application to store files in particular folders using drag and drop. (i am not very experienced so please help)
i now able to search through the main folder and its sub folders, everything ok so far. My search lists the found items in a list box (say all word docs)i use the following code and it opens the main interface but will not allow me to open the word doc. i am doing this modularly so it is a single form.
i have included the whole form the area for concern is:
private void button2_Click_1(object sender, System.EventArgs e)
thank you.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace make_a_dir
{
/// <summary>
/// Summary description for Form7.
/// </summary>
public class Form7 : System.Windows.Forms.Form
{
private ArrayList fileFullNames;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form7()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form7));
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(152, 112);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(232, 121);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexCha nged);
//
// button1
//
this.button1.Image = ((System.Drawing.Image)(resources.GetObject("butto n1.Image")));
this.button1.Location = new System.Drawing.Point(48, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 40);
this.button1.TabIndex = 1;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)) , ((System.Byte)(128)), ((System.Byte)(255)));
this.label1.Font = new System.Drawing.Font("Century Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(8, 272);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(480, 24);
this.label1.TabIndex = 5;
this.label1.Visible = false;
//
// textBox2
//
this.textBox2.Font = new System.Drawing.Font("Century Gothic", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox2.Location = new System.Drawing.Point(32, 112);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(80, 23);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictu reBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(504, 344);
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(240, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 7;
this.button2.Text = "Open";
this.button2.Visible = false;
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// openFileDialog1
//
this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.open FileDialog1_FileOk);
//
// Form7
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 334);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form7";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
if (textBox2.TextLength <=2)
{
MessageBox.Show ("What file are we looking for?");
return;
}
string fileType =textBox2.Text ;
DirRec dr = new DirRec();
//set the start point as the current directory
string theDirectory = (@"c:\TurboFiler");
if ( Directory.Exists ( theDirectory ) == false )
{
MessageBox.Show ( "Directory in textbox does not exist" );
return;
}
//Get Directory Information ... i.e. sub dirs
DirectoryInfo dir = new DirectoryInfo(theDirectory);
// make an array for the list box selection
fileFullNames = new ArrayList ();
//Call to explore the directory and get all the subdirectories.
dr.ExploreDirectory ( dir, listBox1, textBox2, label1, fileFullNames);
button2.Visible =true;
// display end of search
MessageBox.Show (" ******End of search******");
return;
//now focus on the listbox
}
private void button2_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
textBox2.Text =(" ");
listBox1.Visible =true;
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
int item_index = listBox1.SelectedIndex;
//label1.Text = listBox1.Items[item_index].ToString();
label1.Text = fileFullNames[item_index].ToString();
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
}
private void button2_Click_1(object sender, System.EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog" ;
fdlg.InitialDirectory = label1.Text ;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
fdlg.FilterIndex = 2 ;
fdlg.RestoreDirectory = true ;
if(fdlg.ShowDialog() == DialogResult.OK)
{
listBox1.Text = fdlg.FileName ;
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName );
MessageBox.Show(sr.ReadToEnd());
sr.Close();
}
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
}
class DirRec
{
public void ExploreDirectory(DirectoryInfo dir, ListBox listbox1,TextBox textBox2,Label label1,ArrayList fileFullNames)
{
indentLevel++; //Push a directory
///any File Processing should go on around here - I would guess
///
/// Get files in a directory ... Matching a pattern
FileInfo[] allFiles = dir.GetFiles ( "*."+textBox2.Text) ;
//then in a foreach loop much like the one below ...
foreach (FileInfo file in allFiles)
{
/*
* Just add the file to the list box ...
*/
Console.WriteLine("\n\n File Found: {0}", file.FullName);
listbox1.Items.Add ( file.Name );
fileFullNames.Add ( file.FullName );
//get the file info
//now a selected listbox entry which writes to a label
}
DirectoryInfo[] directories = dir.GetDirectories();
foreach (DirectoryInfo newDir in directories)
{
dirCounter++; // increment counter
ExploreDirectory(newDir, listbox1, textBox2,label1, fileFullNames);
}
indentLevel--; //Pop a directory
}
//Static variables to keep track of totals and indentation level
static int dirCounter = 1;
static int indentLevel = -1; // So first push is 0
}
}
}
|