Solved

file association and passing path as string with spaces winforms

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

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 19

Accepted Solution

by:
n2fc earned 500 total points
ID: 40618641
You are removing them here:
path = path.Replace(' " ', '\\');
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 19

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now