By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Problem with System.IO.Path.Combine()

100+
P: 293
HI,
I am getting problem while combining the path "c:" and "File1.txt".The .Net's
Path.Combine('c:","File1.txt") merely returns c:File1.txt hence Path.combine(string path) always retuen false , though file exists in the drive.Can anybody tell me what may b the remeady over this?
Thanks!
Sep 1 '08 #1
Share this Question
Share on Google+
6 Replies


joedeene
100+
P: 583
HI,
I am getting problem while combining the path "c:" and "File1.txt".The .Net's
Path.Combine('c:","File1.txt") merely returns c:File1.txt hence Path.combine(string path) always retuen false , though file exists in the drive.Can anybody tell me what may b the remeady over this?
Thanks!
perhaps adding a forward slash after 'c:' so your code should look like

Expand|Select|Wrap|Line Numbers
  1. System.IO.Path.Combine("C:\", "File1.txt")
right ?

joedeene
Sep 1 '08 #2

100+
P: 293
perhaps adding a forward slash after 'c:' so your code should look like

Expand|Select|Wrap|Line Numbers
  1. System.IO.Path.Combine("C:\", "File1.txt")
right ?

joedeene
Hi,
Thanks for the quick update.But How can i make it more generic..? What if in the function Path.combine(path1,path2) we are having path1 as "c:\\temp1\\" and path2 as "text.txt" then in this can concatinating the path1 with "\" won't
b the solution..?

Thanks!
Sep 1 '08 #3

vekipeki
Expert 100+
P: 229
Look for these methods:

Expand|Select|Wrap|Line Numbers
  1. String.EndsWith (String) 
  2. String.StartsWith (String)
in your .Net documentation, and use them to check whether your string ends or begins with "\\".
Sep 1 '08 #4

P: 25
It only misbehaves when you feed root directory without slash as first argument. Always has as long as I've used it. So does:
Expand|Select|Wrap|Line Numbers
  1. DirectoryInfo dir = new DirectoryInfo("C:")
But both of these will work:
Expand|Select|Wrap|Line Numbers
  1. Path.Combine(@"C:\Some Dir", "File.txt");
  2. Path.Combine(@"C:\Some Dir\", @"File.txt");
I've always assumed it's because C: is not a valid place on the disk, the root starts at C:\ so to speak.
Sep 1 '08 #5

Plater
Expert 5K+
P: 7,872
In the days of DOS and multiple drives, if the "current directory" on C drive was c:\temp\ and you were currently sitting in A:\install\, refering to JUST "c:" would dump you in "c:\temp\" whereas "c:\" would be just that, "c:\".
That was just the way microsoft/dos handled it's mounting system.
If you really look they're all like \{root}\bus[0]\device[0]\hda or something like that in XP.
Sep 2 '08 #6

100+
P: 293
In the days of DOS and multiple drives, if the "current directory" on C drive was c:\temp\ and you were currently sitting in A:\install\, refering to JUST "c:" would dump you in "c:\temp\" whereas "c:\" would be just that, "c:\".
That was just the way microsoft/dos handled it's mounting system.
If you really look they're all like \{root}\bus[0]\device[0]\hda or something like that in XP.

Ohh..

Thanks everybody for much response..
I solved the problem just by concatenating path1 with "\" .Rest is taken care of by path.combine..

Thanks!
Sep 3 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.