Solved

file association and passing path as string with spaces winforms

Posted on 2015-02-18
6
290 Views
Last Modified: 2015-02-19
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
0
Comment
Question by:r3nder
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:n2fc
ID: 40617848
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
0
 
LVL 6

Author Comment

by:r3nder
ID: 40617866
but I am clicking on the filename - it  is associated to the app already - how can I surround the file name with quotes?
0
 
LVL 20

Accepted Solution

by:
n2fc earned 500 total points
ID: 40618641
You are removing them here:
path = path.Replace(' " ', '\\');
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 6

Author Comment

by:r3nder
ID: 40618907
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

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40618914
thanks n2fc
0
 
LVL 20

Expert Comment

by:n2fc
ID: 40619282
Quite welcome... Glad to help!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question