Link to home
Start Free TrialLog in
Avatar of Erwin Pombett
Erwin PombettFlag for Switzerland

asked on

how to recover all uri but the filename ?

hello experts,

i have an Uri object that i'd like to use in order to create a new file.
i'd like to change the last info in the Uri in order to keep the path.

is there an easy way to achieve this ?

example:
Uri test = new Uri(@"c:\path\to\the\file.txt");   // 1. i have an Uri.
ChangeFile(test, "otherFile.txt")                      // 2. change the file for the same uri
Uri test -> c:\path\to\the\otherFile.txt            // 3. now my uri is changed.  

thank you in advance.

 

ASKER CERTIFIED SOLUTION
Avatar of Andre412
Andre412
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops sorry made a small error

Dim folder As String = IO.Path.GetDirectoryName("old path")
Dim filename As String = IO.Path.GetFileName("old path")
Dim NewFilename As String = "predicate" & filename
Dim newlocation As String = IO.Path.Combine(folder, "new filename")

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or the c# version seeing as we are in the c# section

Its better to use the .net path library than to guess the structure will adhere to a predicted format and use string functions

string folder = System.IO.Path.GetDirectoryName("old path");
string filename = System.IO.Path.GetFileName("old path");
string NewFilename = "predicate" + filename;
string newlocation = System.IO.Path.Combine(folder, "new filename");

happy coding :-)
I'm making mistakes all over the place today

string folder = System.IO.Path.GetDirectoryName("old path");
string filename = System.IO.Path.GetFileName("old path");
string NewFilename = "predicate" + filename;
string newlocation = System.IO.Path.Combine(folder, NewFilename );

Open in new window


this could of course be condensed into a one liner


string NewURI = System.IO.Path.Combine(System.IO.Path.GetDirectoryName("old path"), "predicate" + System.IO.Path.GetFileName("old path"));

Open in new window

Avatar of Erwin Pombett

ASKER

thank you a lot experts ;o)

Andre412: no worries for the typos  ;o)

toshi_