0

I have the variable directory which will be a directory that the user inputs, such as "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe". The issue is that the program doesn't recognize the directory, and thus won't launch it via Runtime.getRuntime().exec(new String[] {directory});

How would I make a method/algorithm that takes every \ from directory and turns it into a \\?

if (directory.substring(j, j+1).equals("\\"))
{
    //directory.substring(j, j+1) == "\\\\";
} 

Ex:

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

should turn into

C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Zerukai
  • 137
  • 1
  • 12
  • @RobbyCornelissen Why don't you use `File.Separator` instead of constant notation?? I think you should take a look to this https://stackoverflow.com/questions/27746419/file-separator-vs-file-pathseparator – Vikrant Kashyap Feb 24 '16 at 04:49
  • I don't think you want to do this. There should be exactly one backslash character between each path component. `"\\"` is just a *compile-time representation* of a single backslash. When you type commands (like `cd`) in a Command Window, you don't double the backslashes. – VGR Feb 24 '16 at 04:54
  • @VikrantKashyap I don't need to take a look at anything. Question was: how to replace single backslashes with double backslashes. I don't need to make any further assumptions about OP's operating environment or the non-functional requirements of their application. – Robby Cornelissen Feb 24 '16 at 04:55

2 Answers2

0

You can use replace so directory.replace("\\","\\\\");

-1
string str = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
StringBuilder sb = new StringBuilder(str);
sb.Replace('\\', '\\\');

This replace all ! marks to o i hope this will help u.

Edit:

string str = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
str.Replace('\\', '\\\');
Muhammad Asad
  • 1,772
  • 16
  • 23