Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this invalid file path with // working? [duplicate]

I have this function in a certain Class1:

    public void function1(String path){
  this.excel = new Application();
  this.wbooks = excel.Workbooks;
  this.wb = wbooks.Open(path);
  String rootPath = wb.Path+"//..//..//";
  String nPath = String.Format("{0}//Loads//{1}//{2}",rootPath,name1,name2);
  String outputDir = String.Format("{0}//Input//{1}//{2}", rootPath, name1, name2);
  String erroDir = String.Format("{0}//Erro//{1}//{2}", rootPath, name1, name2);
  for(int i = 0; i < size; i++){
     String[] array2 = File.ReadAllLines(String.Format("{0}//{1}_{2}.txt", nPath, name1, i.ToString()));
     //code
     Directory.CreateDirectory(erroDir);
     File.WriteAllLines(String.Format("{0}//erro_{1}_{2}.txt", erroDir, name1, i.ToString()), array);
     Directory.CreateDirectory(outputDir);
     File.WriteAllLines(String.Format("{0}//output_{1}.txt", outputDir, name2), array);
  }
}

This function from a class and is being called like this in the main function:

String path = "C:\\Users\\myUsername\\Desktop\\myFolder\\";
Class1 temp = new Class1();
temp.function1(path);

Why are the paths made in the function working?Shouldnt path be made "\\" instead of "//".

like image 820
Carlos Siestrup Avatar asked Nov 26 '25 21:11

Carlos Siestrup


1 Answers

Why are the paths made in the function working?Shouldnt path be made "\" instead of "//".

Yes, they should. But Windows is 'smart'. It tries to determine what you actually meant to ask, in this case, if the file path starts with C: for example, it 'knows' it is a local file path, and thus it tries to interpret it as such.

Note that not all programs are that smart. Some .NET build tools for example (not C# itself) are known for some bugs with handling such file paths.

like image 104
Patrick Hofman Avatar answered Nov 29 '25 09:11

Patrick Hofman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!