Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

file association and passing path as string with spaces winforms

I am trying to pass arguments to my application so they have file associations and when the file is double clicked my app opens those files. It all works well and does as it should except when the file name has a space in it - then it says file not found - is there a way to fix this. an example would be.
C:\\Test.itj <----opens
C:\\Test 1.itj <---- doesnt open
here is the file association in the app
           try
            {
                AF_FileAssociator assoc = new AF_FileAssociator(".itj");
                string my_Path = Directory.GetCurrentDirectory();
                // Creates a new file association for the .itj file extension. Data is overwritten if it already exists.
                assoc.Create("FireSight",
                    "Invictus file association",
                    new ProgramIcon(my_Path + "\\Icon.ico"),
                    new ExecApplication(my_Path + "\\FireSight.exe"),
                    new OpenWithList(new string[] { "FireSight.exe" }));
            }
            catch (Exception ex)
            {
                //eat the exception
            }

Open in new window

And here is where I pass  the argument
       static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show(args[0]);
            myMainForm = new Form1();
            if (args.Length == 0)
            {
                Application.Run(myMainForm);
                return;
            }
            else
            {
                string path = Environment.GetCommandLineArgs()[1];
                path = path.Replace('"', '\\');
                path = Path.GetFullPath(path);
                if (File.Exists(path))
                {
                    MessageBox.Show(path);
                    myMainForm.ArgPassed = true;
                    //myMainForm.ReadJobFile(path);
                    myMainForm.strArg = path;
                    Application.Run(myMainForm);
                    
                }
                else
                    MessageBox.Show("path not found");
            }
        }

Open in new window

Does anyone know a fix for this
Avatar of n2fc
n2fc
Flag of United States of America image

Just surround the filename with quotes... That will make it include the spaces as part of the filename!

So it becomes:
"C:\Test.itj"    <----opens
"C:\Test 1.itj" <---- ALSO  opens
Avatar of r3nder

ASKER

but I am clicking on the filename - it  is associated to the app already - how can I surround the file name with quotes?
ASKER CERTIFIED SOLUTION
Avatar of n2fc
n2fc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r3nder

ASKER

Thanks for the help n2fc
here is the final code block - It is being pulled in as a string[] so a simple foreach and remove the line
"path = path.Replace(' " ', '\\');" an voila :D     -thanks
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string val = string.Empty;
            foreach(var arg in args)
            {
                val += arg + " "; 
            }
            //MessageBox.Show(val);
            myMainForm = new Form1();
            if (args.Length == 0)
            {
                Application.Run(myMainForm);
                return;
            }
            else
            {

                string path = val;//Environment.GetCommandLineArgs()[1];
                path = Path.GetFullPath(path);
                if (File.Exists(path))
                {
                    //MessageBox.Show(path);
                    myMainForm.ArgPassed = true;
                    myMainForm.strArg = path;
                    Application.Run(myMainForm);
                    
                }
                else
                    MessageBox.Show("path not found");
            }
        }

Open in new window

Avatar of r3nder

ASKER

thanks n2fc
Quite welcome... Glad to help!