Solved

file association and passing path as string with spaces winforms

Posted on 2015-02-18
6
285 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Service Controller 1 23
Call windows 10 virtual keyboard from windows forms app 2 34
List<PaisEntity> - show some 1 24
C# Gridview 1 29
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 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