The title is confusing, I know.
What I mean is, I created a note pad (like) program with line sorting functions. I want to be able to associate .txt files in explorer with the program. Currently I modified the Main function to accept command like arguments. and I modified my constructor for my main form to load the 1st command line argument as a file name, and open it. so if you drag and drop a file onto the icon, it opens. and works. but if you associate the txt file with the program, the program opens, but the document doesn't. I can't find any articles on this, because Im not sure exactly what to search for.

just for clarification. here is my code.

here's the Entry point

static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new fclsMain(args));
        }
    }

here is the overloaded constructor

public fclsMain(string[] args)
        {
            InitializeComponent();
            if (args.Length == 0)
            {
                currentFile = "";
                this.Text = "SortPad: New Document";
            }
            if (args.Length != 0)
            {
                {
                    currentFile = args[0];

                    System.IO.StreamReader txtReader;
                    txtReader = new System.IO.StreamReader(currentFile);
                    txtMain.Text = txtReader.ReadToEnd();
                    txtReader.Close();
                    txtReader = null;
                    txtMain.SelectionStart = 0;
                    txtMain.SelectionLength = 0;


                    txtMain.Modified = false;
                    FileInfo file = new FileInfo(currentFile);

                    this.Text = "SortPad: " + file.Name;
                    }

                       catch {
                           currentFile = "";
                           this.Text = "SortPad: New Document";

                       }


                }
            }
           
        }

What am I doing wrong?
Is there something that windows adds to the argument when opening a document?

HELP!

Check and see if these are different args for you before the application.run

string[] args = Environment.GetCommandLineArgs();

string[] args = Environment.GetCommandLineArgs();

Im not sure the usage of this. it appears to set the command line arguments to an array of strings called args, isn't that what the overloaded Main method does? I guess I could write a loop to print all the strings in the array to the output pannel and see what windows is doing. I will try that. but I still am not sure about this string[] args = Environment.GetCommandLineArgs(); code. is there a benifit to it over the overladed main method?

thanks,

Don't think there is necessarily any benefit, just want to make sure it is displaying properly.


I think you are leaving some code out, i see a catch without a try.

Add in some debug code like before streaming the file

also make sure currentFile is a string and not an object

MessageBox.Show("going to open: " + currentFile);

Check for the file existing before trying to stream it as well, you don't necessarily want to rely upon a catch block for logic processing

I did leave a little piece out, but I rewrote it using a different method. It turns out that the code would have worked the way it was, windows vista screwed up my associations. even though I select for windows to open a text file with my .exe file, vista attached its self to one instance of it and copied it to the app data hidden user folder. so even though i fixed the code, several times, using multiple methods that should have worked. vista was opening the wrong executable. one i deleted the one vista made, it went crazy. now, even if I publish the program completely and install it with an installer, vista refuses to even try and associate files to my program. even through the control panel applet. does anyone know how to fix that?

and what i settled on doing, although i don't really see any benefit to it, is checking to see if there is any arguments at the entry point and then having 2 constructors for my main form, one with no parameters, and one with a string parameter that contains the file name. I am not 100% sure works correctly, i have to test it on another computer.

any clue about the vista's file association applet refusing my program?

Thanks, Awesome app btw.

I searched the registry and found that there was an entry with the name of my app pointing to the wrong file path, and for some reason the file association wizard wouldn't update it.

It works well now.

I am wondering though, I would like to set file associations programmatically. I understand how to do it in XP, but it doesn't work in vista. I can't find any good tutorials on vista file associations through C#.

any information on that one?
I may just have to start a new thread....

Thanks for the help.

There is a lot of drama to it. after lost of msdn research it turns out that they are both registry entries, but they are in different places, and more over, vista requires a that a special manifest be created to prompt the user account control to ask the user( if they are an admin) to temporarily up the user level to actual administrator rights. that example doesn't work in vista, does in xp though. And it seems that although you can (after jumping through hoops) set vista file associations programmatically, you can't unassociate them programmatically, or at least it doesn't appear possible through my research.

any documentation or examples to prove my research wrong would be a blessing.

This article has been dead for over six months. Start a new discussion instead.