Solved

how to recover all uri but the filename ?

Posted on 2010-11-22
6
417 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_
  • 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 51

Assisted Solution

by:HainKurt
HainKurt earned 125 total points
ID: 34189679
something like this:

int position = uri.LocalPath.LastIndexOf('\\');
String newPath = uri.LocalPath.Right(position) + 'otherFile.txt';
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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