IndexOf gives you the start of the string found in another string. How do
you get the end of the string found?
I am trying to get the path of the startup:
string stemp = Application.StartupPath;
I then want to do something like:
stemp =
stemp.Substring(0,stemp.IndexOf(@"theFolder\")+String.length((@"theFolder\"))
+ "afile.txt";
So that I would end up with something like:
c:\folder1\theFolder\afile.txt
I could do this by putting "theFolder" in a string and then taking the lengh
of the string in my Substring call,
var s = @"theFolder\";
stemp = stemp.Substring(0,stemp.IndexOf(s)+s.Length)) + "afile.txt";
but wanted to see if there was an easier way?
Thanks,
Tom
Gilles Kohl [MVP] - 23 May 2008 17:51 GMT
>IndexOf gives you the start of the string found in another string. How do
>you get the end of the string found?
By adding the length of the search string, which is known.
>I am trying to get the path of the startup:
>
[quoted text clipped - 17 lines]
>
>but wanted to see if there was an easier way?
There is for this type of task, use the methods of the Path class:
(add a "using System.IO;")
string stemp = Application.StartupPath;
string newFileName =
Path.Combine(Path.GetDirectoryName(stemp), "afile.txt");
Regards,
Gilles.
Ignacio Machin ( .NET/ C# MVP ) - 23 May 2008 19:06 GMT
> IndexOf gives you the start of the string found in another string. How do
> you get the end of the string found?
[quoted text clipped - 24 lines]
>
> Tom
Hi,
For working with files and folder you have a couple of classes like
Path , Directory and File
in your case Path.GetDirectoryName is what you are after I think