Hi

]
/DICOMDIR.dic//PACS3.2.0/Images

my problem is that I want to get only the specific part of this URL such as only .dic
for eg:
]
1.2.840.114257.0.11796824240075870457700001003416408300001
/DICOMDIR.dic

and remove the last two item from URL. please help

Dani AI

Generated

confirmed this is a server-side URL issue (as asked), so treat the value as an absolute URI and parse its path into segments instead of doing ad-hoc substring work. That makes the code robust against extra slashes, query strings, and different host/port combinations.

A reliable pattern in C# is to use System.Uri to get AbsolutePath, split on '/' while removing empty entries, then rebuild the path without the last two segments. Example:

string url = "http://yourserver/.../UID/filename.ext/extra/segments";
var uri = new Uri(url);
var segments = uri.AbsolutePath.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);

// make sure there are at least two segments to remove
if (segments.Length >= 2)
{
    var kept = segments.Take(segments.Length - 2);
    var newPath = "/" + string.Join("/", kept);
    var rebuilt = $"{uri.Scheme}://{uri.Host}{(uri.IsDefaultPort ? "" : ":" + uri.Port)}{newPath}";
    // rebuilt is the URL with the last two path items removed
}

To extract a filename extension (for example the .dic at the end of a file segment) use System.IO.Path.GetExtension on the last non-empty segment:

var last = segments.LastOrDefault() ?? "";
var ext = Path.GetExtension(last); // returns ".dic" if the last segment is a file named *.dic

Pitfalls and checks: normalize multiple consecutive slashes (RemoveEmptyEntries handles most cases), handle query/fragment (uri.AbsolutePath excludes them), validate segments.Length before indexing, decode percent-encoding with Uri.UnescapeDataString if needed, and handle relative URLs by constructing a base Uri. If the input sometimes lacks a scheme/host, parse it as a path and apply similar split/join logic rather than relying on new Uri(...) to fail.

Recommended Answers

All 2 Replies

Where is this coming from in your application , is it client side or server side be a bit specific

Hi Sarama,
Url Come from server side

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.