How to correctly connect the file name with the path to the file?

Closed due to the fact that the essence of the question is not clear to the participants of Denis , Kromster , Denis Bubnov , Alex , user194374 24 Dec '16 at 8:13 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • In my opinion this is it java2s.com/Tutorials/CSharp/System.IO/Path/… - Alexander Muksimov
  • string path = @ "C: \ temp \ file.txt"; are you talking about it? - ogorank
  • And for what cons? Good simple question, not? - VladD
  • The answers are good, especially with exception handling. Took note. - Alexander Puzanov

2 answers 2

Use the Path.Combine method ( System.IO namespace). Example:

 string fullPath = Path.Combine(@"C:\Log", "log.txt"); 

    If you need an exception to be thrown in the case of "wrong" names:

      if (Path.GetFileName(fileName) != fileName) { throw new Exception("'fileName' is invalid!"); } string combined = Path.Combine(dir, fileName); 

    If you just need to smerdit not bothering:

      string combined = Path.Combine(dir, Path.GetFileName(fileName));