locked
Backslash works in debug mode but fails during execution RRS feed

  • Question

  • this works

                absolutePath = @"C:\Users\JavaMan\Desktop\CameraTotal\CameraDesktop\CameraDesktop\bin\Debug\Camera.lnk";
                Process.Start(@absolutePath);

    but this fails even though the absolutePath in both cases is exactly the same

               

                String absolutePath = Directory.GetCurrentDirectory();

                char c = (char)92; //backslash
                absolutePath = absolutePath + c + "Camera.lnk";
                Process.Start(@absolutePath);

    PeaceMan

    Friday, September 16, 2016 3:44 PM

Answers

  • Try this and see if you get any MessageBox.

    string path = string.Empty;
    try
    {
        string a = @"C:\Users\Computer";
        string b = @"Camera.lnk";
        path = Path.Combine(a, b);
        Process.Start(path);
        MessageBox.Show("File Opened Successfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(path + Environment.NewLine + ex.ToString())
    }


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    • Marked as answer by GolfMan Saturday, September 17, 2016 10:51 AM
    Saturday, September 17, 2016 3:40 AM

All replies

  • How about

    String absolutePath = Path.Combine(Directory.GetCurrentDirectory(), "Camera.lnk");

    Or

    string fileName = "Camera.lnk";
    String absolutePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, September 16, 2016 4:18 PM
  • this works

                absolutePath = @"C:\Users\JavaMan\Desktop\CameraTotal\CameraDesktop\CameraDesktop\bin\Debug\Camera.lnk";
                Process.Start(@absolutePath);

    but this fails even though the absolutePath in both cases is exactly the same

               

                String absolutePath = Directory.GetCurrentDirectory();

                char c = (char)92; //backslash
                absolutePath = absolutePath + c +;
                Process.Start(@absolutePath);

    PeaceMan

    If you want to combine to path then following is the correct way

    System.IO.Path.Combine(absolutePath,"Camera.lnk");
    Also you don't require "@" with absolutepath in Process.Start.
    Process.Start(absolutePath);



    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Friday, September 16, 2016 4:21 PM
  • very nice however it also fails. Even though they produce the same exact string as the hard coded path.

    the only thing that works is if I explicitly hard code the path. I have also tried building the string using Replace, Split, (Char)92, @"\", string[], Char[] etc...


    PeaceMan

    Friday, September 16, 2016 4:53 PM
  • I removed the @ and still fails during Execution mode but runs successfully in Debug mode. if I hard code the path then it works in both modes

    PeaceMan

    Friday, September 16, 2016 4:59 PM
  • I removed the @ and still fails during Execution mode but runs successfully in Debug mode. if I hard code the path then it works in both modes

    PeaceMan

    Do you get any error/exception message? If yes then post the exception details.

    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Friday, September 16, 2016 5:43 PM
  • no exceptions. when I run the vs2015 under debug mode it works fine with either coding. if I run the executable then  only hard coded path works with no exception on the Combine command.

    PeaceMan

    Friday, September 16, 2016 6:28 PM
  • no exceptions. when I run the vs2015 under debug mode it works fine with either coding. if I run the executable then  only hard coded path works with no exception on the Combine command.

    PeaceMan

    See the following, sounds like a permission issue in that Visual Studio under debug mode you have permissions but not when running outside of debug mode.

    http://stackoverflow.com/questions/133379/elevating-process-privilege-programatically


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, September 16, 2016 6:46 PM
  • I don't understand...If you have some solution I would appreciate the code examples as the article does not explain the following

    this works under debug as well double clicking the .exe file

    string path = @"C:\Users\Computer\Camera.lnk";

    Process.Start(path);

    this works in debug mode but does not execute by double clicking .exe file

    string a = @"C:\Users\Computer";

    string b = @"Camera.lnk";

    string path = Path.Combine(a, b);

    Process.Start(path);


    PeaceMan

    Friday, September 16, 2016 8:33 PM
  • Try this and see if you get any MessageBox.

    string path = string.Empty;
    try
    {
        string a = @"C:\Users\Computer";
        string b = @"Camera.lnk";
        path = Path.Combine(a, b);
        Process.Start(path);
        MessageBox.Show("File Opened Successfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(path + Environment.NewLine + ex.ToString())
    }


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    • Marked as answer by GolfMan Saturday, September 17, 2016 10:51 AM
    Saturday, September 17, 2016 3:40 AM
  • Brilliant...It works with!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    This was a very difficult problem for you to solve!!!!!!!!!!!

    I am extremely impressed!!!!!!!!!!!!!!!!!!!!

    problem is solved with this

    string path = string.Empty;

    but problem failed with

    string path;

    Please explain

    now finally within your solution

    string a = Directory.GetCurrentDirectory();

    does not work...please provide a solution


    PeaceMan

    Saturday, September 17, 2016 4:52 AM
  • I guess path of Directory.GetCurrentDirectory() is different and not C:\Users\Computer. Try following code. You will get path of current directory in MessageBox.

    string path = string.Empty;
    try
    {
        string a = Directory.GetCurrentDirectory();
        MessageBox.Show(a);
        string b = @"Camera.lnk";
        path = Path.Combine(a, b);
        Process.Start(path);
        MessageBox.Show("File Opened Successfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(path + Environment.NewLine + ex.ToString())
    }


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Saturday, September 17, 2016 5:33 AM
  • its remarkable and I would have never thought of putting messagebox for string a.

    In debug mode I get

    a=C:\Users\Computer

    but when I run .exe for the same code I get

    a=C:\Windows\System32

    so here is what I did that works for both debug and .exe

    a = (new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location)).Directory.FullName;

    EXCEPTIONAL work and support!!!!!!!!!


    PeaceMan

    Saturday, September 17, 2016 10:50 AM