Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

file association and passing path as string with spaces winforms

Posted on 2015-02-18
6
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 40618641
You are removing them here:
path = path.Replace(' " ', '\\');
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Progress
Starting up a Project

715 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