Solved

how to recover all uri but the filename ?

Posted on 2010-11-22
6
420 Views
Last Modified: 2012-05-10
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.

 

0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 4

Accepted Solution

by:
Andre412 earned 125 total points
ID: 34189626
hi

use a combination of the following as you eed

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

Open in new window

0
 
LVL 4

Expert Comment

by:Andre412
ID: 34189649
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

0
 
LVL 54

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 125 total points
ID: 34189679
something like this:

int position = uri.LocalPath.LastIndexOf('\\');
String newPath = uri.LocalPath.Right(position) + 'otherFile.txt';
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 4

Expert Comment

by:Andre412
ID: 34189765
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 :-)
0
 
LVL 4

Expert Comment

by:Andre412
ID: 34189814
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

0
 

Author Comment

by:toshi_
ID: 34189969
thank you a lot experts ;o)

Andre412: no worries for the typos  ;o)

toshi_
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question