9 Replies - 1990 Views - Last Post: 17 March 2012 - 03:39 PM Rate Topic: -----

#1 hondakillrsx  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 08-March 12

Open Text File when double-clicked

Posted 16 March 2012 - 10:40 AM

Alright,
so I'm trying to get my notepad program that I just created to open whichever file was double-clicked. I changed the default program in windows to the exe of my new program, but it only opens the program itself and not the file in it. I'm assuming I need to a statement when loading the program to "File.WriteAllText" to the box1.Text, no?
But I don't know how to call the file path of the file that was doubleclicked outside of the program. I'm assuming I need something like this:

public Form1()
{
InitializeComponent();
//string filename;
//filename = file path that was double clicked
if (filename == null)
//open program.....
if (filename > 0)
//
File.WriteAllText(filename.FileName, box1.Text);
}



something to that effect?

Is This A Good Question/Topic? 0
  • +

Replies To: Open Text File when double-clicked

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,461
  • Joined: 02-June 10

Re: Open Text File when double-clicked

Posted 16 March 2012 - 11:02 AM

I've never done this, but I think it more likely that your program needs to first receive the file path from the OS when you double-click the data file. Otherwise, how would it know what to open?

I'm pretty sure you need to supply the path as command line arguments, check if the file exists, then open it.
Was This Post Helpful? 2
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1210
  • View blog
  • Posts: 4,124
  • Joined: 27-January 10

Re: Open Text File when double-clicked

Posted 16 March 2012 - 11:25 AM

I agree with Til'q. You need to supply command line arguments.

Step 1:
How do I pass in command line arguments to an application?

Step 2:
How do I use this command line argument to pass the string "path" to my Form1.cs class?

The rest will come easily.
Was This Post Helpful? 1
  • +
  • -

#4 hondakillrsx  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 08-March 12

Re: Open Text File when double-clicked

Posted 16 March 2012 - 01:12 PM

Thanks Guys.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: Open Text File when double-clicked

Posted 16 March 2012 - 03:58 PM

In Windows Forms apps, it's probably easier to use Environment.GetCommandLineArgs than it is to use the argument in Main, but the point is the same.

You'll need to check to see if there are any arguments, and if there are, the first one should be the file path.
Was This Post Helpful? 0
  • +
  • -

#6 hondakillrsx  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 08-March 12

Re: Open Text File when double-clicked

Posted 16 March 2012 - 11:08 PM

Guys, help me out! I'm going crazy!

So i've come up with this:

 
  try
            {
                String[] args = Environment.GetCommandLineArgs();
                var message = string.Join(" ", args);

                InitializeComponent();
                
                File.WriteAllText(message, box1.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



If I run this, the catch throws a "The given file path format is not supported". What I think is happening is the writer is using the first part of the Args which is the path to the program.exe. I need to be able to hide this and just have the file path to the file that is being double clicked. IM SO CLOSE!
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: Open Text File when double-clicked

Posted 16 March 2012 - 11:17 PM

It's an array. Don't join the whole thing together, just use the one index that you need. Also, why would you be writing to the file you're double-clicking? Shouldn't you be reading from it?
Was This Post Helpful? 1
  • +
  • -

#8 hondakillrsx  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 08-March 12

Re: Open Text File when double-clicked

Posted 17 March 2012 - 09:43 AM

View PostCurtis Rutland, on 16 March 2012 - 11:17 PM, said:

It's an array. Don't join the whole thing together, just use the one index that you need. Also, why would you be writing to the file you're double-clicking? Shouldn't you be reading from it?


Ok so this is what I've got now. Not sure why I had the Writing, but yes you were right. So this works when opening a document. It does what it should. But when I try to just open the program I get an "index outside the scope of the array". So I'm sure that it has something to do with how I wrote the the "if" statement. Can anyone think of another way to get the if statement to just open the program normally if a file hasn't been doubleclicked?

public Form1()
        {
            try
            {
                String[] args = Environment.GetCommandLineArgs();
                filenamE = args[1];
                if (args[1] == null)
                {
                    InitializeComponent();
                }
                else
                {
                    InitializeComponent();
                    box1.Text = File.ReadAllText(filenamE);
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: Open Text File when double-clicked

Posted 17 March 2012 - 09:48 AM

Well, do yourself and us a favor:

string[] args = Environment.GetCommandLineArgs();
for(int i=0; i<args.Length; i++){
  box1.Text += string.Format("{0}: {1}{2}", i, args[i], Environment.NewLine);
}


Use that code instead of what you've got. That'll print each index of the array into your textbox. Find the one that works, and is correct, then use that index in your program.

Also, you're going to have to check to see if the args exist at all. The way you're trying to do that won't work (because indexes outside the bounds of an array throw an exception, they don't return null). You'll have to check the array's Length property.
Was This Post Helpful? 1
  • +
  • -

#10 hondakillrsx  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 08-March 12

Re: Open Text File when double-clicked

Posted 17 March 2012 - 03:39 PM

This is the final method that did it(Thanks to you all):
  try
            {
                InitializeComponent();
                string[] args = Environment.GetCommandLineArgs();
                for (int i = 0; i < args.Length; i++)
                {
                    filenamE = args[i];
                    if (i > 0)
                    {   
                        box1.Text = File.ReadAllText(filenamE);
                    }
                    else
                    { 
                    }
                    
                }
            }



Thanks again.

-Lane
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1