Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 13th, 2005, 06:10 AM
Registered User
 
Join Date: Apr 2005
Location: London, , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
        }

    }
}

Reply With Quote
  #2 (permalink)  
Old April 20th, 2005, 05:54 AM
Friend of Wrox
 
Join Date: Feb 2004
Location: Bangalore, Karnataka, India.
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Word Documents you cannot open liek this, See the following code to open, read and display the contents in text box.

First add a reference to Microsoft.Office.Core in the project
Make the TextBox multi line property to True, then add the execute the following code to load a word document to the text box.


object fileName = @"test.DOC";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();

Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing,ref missing,ref missing, ref missing);

Word._Document WordAppDoc = new Word.DocumentClass ();
WordAppDoc.Application = oWordApp;

textBox1.Text = oWordDoc.Content.Text.ToString ();




It is not how much we do,
but how much love we put in the doing.

-Mother Theresa
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening PDF files comicghozt ASP.NET 1.0 and 1.1 Professional 11 October 23rd, 2006 04:06 AM
Please Help! Opening Multiple files jezywrap Excel VBA 3 December 19th, 2005 12:34 PM
File Associations and Opening FIles snowy0 VB.NET 2 October 8th, 2004 04:56 PM
Opening Files Read-Only SerranoG Access VBA 5 May 6th, 2004 02:40 PM



All times are GMT -4. The time now is 12:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.