Small Problem

  • Thread starter Thread starter JLW
  • Start date Start date
J

JLW

Here's my variables:
string FileName
string[] tmpSpl
string FilePath

Now, FileName has full path/filename, so, how do I Split FileName at the "\"
char into tmpSpl, then redim preserve tmpSpl by one less, the rejoin it all
into FilePath. I can do this very easily in VB.NET, but can't find anything
on how to do it in C#

Thanks,
JLW
 
I am not eactly sure what you are trying to do. but you should look in to
the System.IO.Path object.

using System.IO; // at the top

example 1:
string fliename = @"C:\test\mysong.mp3";
Path.GetPathRoot(filename); // c:\test
Path.GetFileName(filename); // song.mp3
Path.ChangeExtension(filename, ".wav"); // c:\test\song.wav
Path.GetDirectoryName(filename); // returns test
Path.GetFileNameWithoutExtention(filename); // returns song

exmple 2:
string path = "c:\test";
string path2 = "c:\test\";
string file = "song.mp3";

Path.Combine(path, file); // returns C:\test\mysong.mp3
Path.Combine(path2, file); // also returns C:\test\mysong.mp3


For you problem I would probably use
string[] rootPath = Path.GetDirectoryName(filename).Split('\\');
 
JLW said:
Here's my variables:
string FileName
string[] tmpSpl
string FilePath

Now, FileName has full path/filename, so, how do I Split FileName at the "\"
char into tmpSpl, then redim preserve tmpSpl by one less, the rejoin it all
into FilePath. I can do this very easily in VB.NET, but can't find anything
on how to do it in C#

Thanks,
JLW

Look in .NET docs for:

// join & split
String.Join(...)
string.Split(...)

// redim
Array.Copy(...)

// and ;)
Path.DirectorySeparatorChar

Cheers!

Marcin
 
The problem I'm having with split is "\" is a line terminantor char, and I'm
getting just that error. This is all in a function that is being passed the
full path/filename as an argument, and I put a break on the function, and it
doesn't come acrossed as "C:\\path\\file" but "C:\path\file", so that just
isn't working right.

Thanks again,
JLW
Marcin Grzêbski said:
JLW said:
Here's my variables:
string FileName
string[] tmpSpl
string FilePath

Now, FileName has full path/filename, so, how do I Split FileName at the "\"
char into tmpSpl, then redim preserve tmpSpl by one less, the rejoin it all
into FilePath. I can do this very easily in VB.NET, but can't find anything
on how to do it in C#

Thanks,
JLW

Look in .NET docs for:

// join & split
String.Join(...)
string.Split(...)

// redim
Array.Copy(...)

// and ;)
Path.DirectorySeparatorChar

Cheers!

Marcin
 
Try Split("\\")

JLW said:
The problem I'm having with split is "\" is a line terminantor char, and I'm
getting just that error. This is all in a function that is being passed the
full path/filename as an argument, and I put a break on the function, and it
doesn't come acrossed as "C:\\path\\file" but "C:\path\file", so that just
isn't working right.

Thanks again,
JLW
Marcin Grzêbski said:
JLW said:
Here's my variables:
string FileName
string[] tmpSpl
string FilePath

Now, FileName has full path/filename, so, how do I Split FileName at
the
it
all

Look in .NET docs for:

// join & split
String.Join(...)
string.Split(...)

// redim
Array.Copy(...)

// and ;)
Path.DirectorySeparatorChar

Cheers!

Marcin
 
AFter playing around, this works:
Path.GetDirectoryName(FileName)
Thanks again guys, it was really starting to drive me nutz!

JLw

Ben Dewey said:
I am not eactly sure what you are trying to do. but you should look in to
the System.IO.Path object.

using System.IO; // at the top

example 1:
string fliename = @"C:\test\mysong.mp3";
Path.GetPathRoot(filename); // c:\test
Path.GetFileName(filename); // song.mp3
Path.ChangeExtension(filename, ".wav"); // c:\test\song.wav
Path.GetDirectoryName(filename); // returns test
Path.GetFileNameWithoutExtention(filename); // returns song

exmple 2:
string path = "c:\test";
string path2 = "c:\test\";
string file = "song.mp3";

Path.Combine(path, file); // returns C:\test\mysong.mp3
Path.Combine(path2, file); // also returns C:\test\mysong.mp3


For you problem I would probably use
string[] rootPath = Path.GetDirectoryName(filename).Split('\\');


JLW said:
Here's my variables:
string FileName
string[] tmpSpl
string FilePath

Now, FileName has full path/filename, so, how do I Split FileName at the "\"
char into tmpSpl, then redim preserve tmpSpl by one less, the rejoin it all
into FilePath. I can do this very easily in VB.NET, but can't find anything
on how to do it in C#

Thanks,
JLW
 
JLW,

Just to let you know when working with strings that have backslashes there
are two ways of doing it.

1. "C:\\test\\somefile.txt";
2. @"C:\test\somefile.txt";

This should help you with some of your escaping issues.


JLW said:
AFter playing around, this works:
Path.GetDirectoryName(FileName)
Thanks again guys, it was really starting to drive me nutz!

JLw

Ben Dewey said:
I am not eactly sure what you are trying to do. but you should look in to
the System.IO.Path object.

using System.IO; // at the top

example 1:
string fliename = @"C:\test\mysong.mp3";
Path.GetPathRoot(filename); // c:\test
Path.GetFileName(filename); // song.mp3
Path.ChangeExtension(filename, ".wav"); // c:\test\song.wav
Path.GetDirectoryName(filename); // returns test
Path.GetFileNameWithoutExtention(filename); // returns song

exmple 2:
string path = "c:\test";
string path2 = "c:\test\";
string file = "song.mp3";

Path.Combine(path, file); // returns C:\test\mysong.mp3
Path.Combine(path2, file); // also returns C:\test\mysong.mp3


For you problem I would probably use
string[] rootPath = Path.GetDirectoryName(filename).Split('\\');


JLW said:
Here's my variables:
string FileName
string[] tmpSpl
string FilePath

Now, FileName has full path/filename, so, how do I Split FileName at
the
"\"
char into tmpSpl, then redim preserve tmpSpl by one less, the rejoin
it
all
into FilePath. I can do this very easily in VB.NET, but can't find anything
on how to do it in C#

Thanks,
JLW
 
Back
Top