|
List Info
Thread: C-Sharp (C#) Group: String concatenation
|
|
| C-Sharp (C#) Group: String concatenation |

|
2006-01-04 14:30:09 |
I have tried various ways to concatenate and folder path and
a file
name, eg:
C:Temp + MyFile.txt => C:TempMyFile.txt.
I cannot seem to previous getting the result
C:Temp\MyFile.txt. The
folder path is provided as a parameter called Path (it does
not end in
a back-slash). I have tried:
Path + "MyFile.txt";
Path
+ "MyFile.txt";
string.Concat(Path, "MyFile.txt");
string.Format( "",Path, "MyFile.txt");
Where am I going wrong?
Thanks in advance ...
|
|
| C-Sharp (C#) Group: Re: String
concatenation |

|
2006-01-04 15:00:14 |
|
Use Path.Combine(string path1, string path2). It will return a string that combines a root path with a subpath.
For example:
Path.Combine( "C:Temp", "MyFile.txt") will return "C:TempMyFile.txt".
Path.Combine( "C:Temp", "subfolder1subfolder2MyFile.txt") will return C:Tempsubfolder1subfolder2MyFile.txt
Path.Combine is useful because you don't have to worry about the slash path delimiters. Both of these will combine into the same string:
Path.Combine( "C:Temp", "subfolder1MyFile.txt") Path.Combine( "C:Temp", "subfolder1MyFile.txt")
|
| C-Sharp (C#) Group: Re: String
concatenation |

|
2006-01-05 13:35:49 |
Thanks for your help, Marty. I still have a problem,
though. The path
to the folder is passed as a string parameter called
FolderPath. It
has value "c:temp". I still get two
back-slashes:
Path.Combine(FolderPath, "myfile.txt")
returns c:temp\myfile.txt.
But Path.Combine( "c:temp", "myfile.txt")
returns c:tempmyfile.txt.
The latter does just what I want, but if I was hard-coding
both the
path and file name, I wouldn't need to concatenate!?
Any ideas on how to use the FolderPath parameter?
Thank you,
David
|
|
| C-Sharp (C#) Group: Re: String
concatenation |

|
2006-01-05 15:27:27 |
|
I don't see how it could be doing that unless the FolderPath variable contained C:temp\ before trying to combine the path. Try this:
Path.Combine(FolderPath.TrimEnd('\'), "myfile.txt");
Also try stepping through the debugger and seeing what the exact value of FolderPath is before you combine it.
On 1/5/06, Davy B < david.bridge itv.com">david.bridge itv.com> wrote:
Thanks for your help, Marty. I still have a problem, though. The path to the folder is passed as a string parameter called FolderPath. It has value "c:temp". I still get two back-slashes:
Path.Combine
(FolderPath, "myfile.txt") returns c:temp\myfile.txt.
But Path.Combine( "c:temp", "myfile.txt") returns c:tempmyfile.txt.
The latter does just what I want, but if I was hard-coding both the
path and file name, I wouldn't need to concatenate!?
Any ideas on how to use the FolderPath parameter?
Thank you,
David
|
| C-Sharp (C#) Group: Re: String
concatenation |

|
2006-01-05 15:41:21 |
I am passing through a raw value of "c:temp". I
have stepped through
and verified that the param is definitely set to the right
value.
This is the line that instantiates my Settings class:
Settings settings = new Settings("c:temp");
And this is the Settings constructor (start of it):
public Settings(string FolderPath)
{
// Open settings file and set values
_path = Path.Combine(FolderPath, "maitreD.ini");
// value of
FolderPath is "c:path". Line produces
"c:path\maitreD.ini"?
// _path = Path.Combine( "c:Temp", "maitreD.ini"); this works ok
if (File.Exists(_path))
I have hovered the mouse over the FolderPath param, and its
value is
"c:temp". I am baffled as to where the
double-backslashes are coming
from. I shall try this on my home network to see if I get
the same
result. Perhaps it's something to do with the WinXP
Professional
config at work? We're on a Novell network, which
occasionally exhibits
strange behaviour?
I have tried your TrimEnd suggestion too, with the same
result.
Thanks for your continued interest and help,
David
|
|
| C-Sharp (C#) Group: Re: String
concatenation |

|
2006-01-05 15:51:16 |
|
Although this wouldn't be causing the exact issue you are describing, the line:
Settings settings = new Settings("c:temp");
should be causing another problem. That backslash in the string parameter should be escaped by either putting the sign in front of the literal, or by doing a double \. The escape sequence of t resolves to a tab character. But still, that shouldn't be causing the problem you're having.
I have made a class similar to yours, and everything works fine. Here is how it looks:
class Class1 { /// <summary> /// The main entry point for the application. /// </summary>
[STAThread] static void Main(string[] args) { Settings s = new Settings( "C:temp"); Console.WriteLine(s.FilePath); Console.Read();
} }
class Settings { private string _path;
public Settings(string FolderPath) { _path = Path.Combine(FolderPath, "MyFile.txt");
}
public string FilePath { get { return this._path; } } }
|
| C-Sharp (C#) Group: Re: String
concatenation |

|
2006-01-05 17:15:51 |
You were spot on! I should have prefixed the parameter with
.
Works
a treat now. Why, without the it should have put a
double-backslash
at the end of the string I cannot imagine. I was passing in
an
escape-colon. Perhaps this has a special meaning?
Many thanks!
|
|
[1-7]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|