How to retrieve characters before and after a forward slash - c#

I have this string which comes from the datareader: http://win-167nve0l50/dev/dev/new/1st Account
I want the get the file name, which is "1st Account"
and the list name, which is "new" and the list address which is "http://win-167nve0l50/dev/dev/". This is the code I am using:
How do I retrieve the site address and the list name.
//getting the file URL from the data reader
string fileURL = dataReader["File URL"].ToString();
//getting the list address/path
string listAdd = fileURL.Substring(fileURL.IndexOf("/") + 1);
//getting the file name
string fileName = fileURL.Substring(fileURL.LastIndexOf("/") + 1);

You can get all sorts of info easily using the Uri Class
var uri = new Uri(dataReader["File URL"].ToString());
then you can get various bits from the Uri object eg.
Uri.Authority - Gets the Domain Name System (DNS) host name or IP address and the port number for a server.
Uri.Host - Gets the host component of this instance
Uri.GetLeftPart() - Gets the specified portion of a Uri instance.

If dealing with Uri, use the according class ...
Uri u = new Uri(dataReader["File URL"].ToString());
... and access the desired parts of the path by Segments array
string listAdd = u.Segments[3]; // remove trailing '/' if needed
string fileName = u.Segments[4];
... or in case you need to make sure to handle arbitrary path length
string listAdd = u.Segments[u.Segments.Length - 2];
string fileName = u.Segments[u.Segments.Length - 1];

You should use the Uri class
It's main purpose is to handle composed resource strings.
You can use:
Uri.Host to obtain the host address string
PathAndQuery to getthe absolute path of the file on the server and the query information sent with the request
All the properties here

You can also do it with Regex:
string str = "http://win-167nve0l50/dev/dev/new/1st Account";
Regex reg = new Regex("^(?<address>.*)\\/(?<list>[^\\/]*)\\/(?<file>.*)$");
var match = reg.Match(str);
if (match.Success)
{
string address = match.Groups["address"].Value;
string list = match.Groups["list"].Value;
string file = match.Groups["file"].Value;
}

Related

How to remove bracketed sections of a Url

In my c# application I am building a routine that will parse through a url replace any sections of the url with the appropriate data.
For instance, if I have a url like:
api.domain.com/users/{id}
and the user provides the id, i replace the id with the given value.
This is simple enough:
if(path.Contains("{id}") path = path.Replace("{id}", id);
However, what I want is to be able to remove the {id} from the url if no id is provided so that the final url is:
api.domain.com/users
I would also want it to intelligently be able to remove items in the middle of the path so that if the url were:
api.domain.com/users/{id}/photos
I would get:
api.domain.com/users/photos
For this, I would not know the text of the key ahead of time so {id} could be anything such as:
{name} {sometext} {anyvalue}
But I do know that each MUST be contained in curly braces.
Any help would be greatly appreciated!
You can do something like this:
public string UrlReplace(string Url, string key, string value = "")
{
var replaceString = "{" + key + "}"; //get the string to replace
//if the value is empty remove the slash
if (string.IsNullOrEmpty(value))
{
//find the index of the replace string in the url
var index = url.IndexOf(replaceString) + replaceString.Length;
if (url[index] == '/')
{
url = url.Remove(index, 1); //if the character after the string is a slash, remove it
}
}
return url.Replace(replaceString, value); //replace the string with the value
}
Then you'd use it like
string url = "api.domain.com/users/{id}/photos";
url = UrlReplace(url,"id","test");
//returns "api.domain.com/users/test/photos"
url = UrlReplace(url, "id", "");
//returns "api.domain.com/users/photos"

Special chars in query string

I am passing a query string parameter containing file name.
default.aspx?file=Fame+ adlabs.xml (Fame+ adlabs.xml is the actual file name on server). The file name has "+" & also blank spaces.
When I check for file name from query string as follows:
var fileName = Request.QueryString["file"];
The variable filename does not have a "+" in it. It reads as "Fame adlabs.xml" & hence I get a file not found exception. I cannot rename the xml files. Can someone please guide me into right direction.
Thanks
If you are trying to do it at the server in C#:
String FileName = "default.aspx?";
String FullURL = FileName + HttpUtility.UrlEncode("Fame + adlabs.xml");
String Decoded = HttpUtility.UrlDecode(FullURL);
You should URL encode into your javascript before sending it :
var name = "Fame+ adlabs.xml";
var url = "default.aspx?file=" + encodeURIComponent(name);
Pay attention that following char won't work : ~!*()'

Add a directory name to a URL address using C#

I have a textbox where users can paste a URL address. I want to add a directory name to the URL before saving it in the database.
<asp:TextBox ID="urlTextbox" runat="server"></asp:TextBox>
Code behind
TextBox url = urlTextbox as TextBox;
string urlString = urlTextbox.Text;
Let's say the urlString = "mydomain.com/123456". I want to replace it with "mydomain.com/directory/123456". mydomain.com/directory is the same for all the URLs. The last part "123456" changes only.
Thank you
I'd suggest seeing if your needs are met with the UriBuilder class.
UriBuilder url = new UriBuilder(urlTextbox.Text);
Now you can use the various properties to change your url.
string formattedUrl = string.Format("{0}://{1}/directory/{2}", url.Scheme, url.Host, url.Path);
A better idea is to adjust the URL with another / same UriBuilder as noted by Jared.
UriBuilder url = new UriBuilder(urlTextbox.Text);
url.Path = string.Format("directory/{0}", url.Path);
Use this object as a Uri by simply doing this
Uri formattedUrl = url.Uri;
Or convert to a string if needed.
string formattedUrl = url.ToString();
You can also use Uri.TryParse(...) to verify if it's a valid URL being entered into the text box.
To get the individual query parameters, you can look at the Uri object.
UriBuilder url = new UriBuilder("mydomain.com/123456?qs=aaa&bg=bbb&pg=ccc");
url.Path = string.Format("directory/{0}", url.Path);
Uri formattedUrl = url.Uri;
string queryString = formattedUrl.Query;
// parse the query into a dictionary
var parameters = HttpUtility.ParseQueryString(queryString);
// get your parameters
string qs = parameters.Get("qs");
string bg = parameters.Get("bg");
string pg = parameters.Get("pg");
You can use string functions Split and Join to achieve your result. An example code is shown below
List<string> parts = urlString.Split(new char[] { '/'}).ToList();
parts.Insert(parts.Count - 1, "directory");
urlString = string.Join("/", parts);
This is one way of doing. Split the urlString using .split() function.
string[] parts = urlString.Split('/');
parts[parts.Length-1] will have that number. Append it to the string you want.
I'd do something like this:
//Assuming the address in urlString has the format mydomain.com/123456
string[] urlParts = urlString.Split('/');
string directory = "directory";
string finalUrl = urlParts[0] + "/" + directory + "/" + urlParts[1];
Be careful if the address has other "/" characters, like if preceded by http:// or something like that.
Hope it helps.
Simply use concatenation:
save in a temporary string
temp="mydomain.com/directory/"
and save the changing part in another string like
temp2="123456"
now concatenate both temp1 and temp2 like below.
urlString=temp1+temp2;

C# getting first Characters of a file name

I have a directory name "C:\Folder\160_Name_2013111914447.7z" what I need is to extract the "160" from the file name in C# and use it to pass it to a MS-SQL method so I can move the file to a correct file Namely "160".
Please help, as I'm kinda new to C#.
Try something like this:
Path.GetFileName(#"C:\Folder\160_Name_2013111914447.7z").Split('_')[0];
Or possibly
string fileName = Path.GetFileName(#"C:\Folder\160_Name_2013111914447.7z");
Regex.Match(fileName, "^([0-9]+)_").Groups[1].Value;
If you need to take the first 3 symbols, you can use the Substring method of the string class:
string fileName = Path.GetFileName(#"C:\Folder\160_Name_2013111914447.7z");
// take 3 symbols starting from 0th character.
string extracted = fileName.Substring(0, 3);
If you can have variable length of key characters and the underscore character is the separator, then we'll have to modify the above code a little. First, we'll need the index of the underscore:
string fileName = Path.GetFileName(#"C:\Folder\160_Name_2013111914447.7z");
// get the zero-based index of the first occurrence of an underscore:
int underscoreIndex = fileName.IndexOf("_");
The string.IndexOf(...) methods returns -1 if match is not found, so we need to check for it.
if (underscoreIndex >= 0)
{
string extracted = fileName.Substring(0, underscoreIndex);
}
else
{
// no underscore found
throw new InvalidOperationException(
"Cannot extract data from file name: " + fileName);
}
To get the number assuming the file path you input will always be at the start and a length of 3 characters you can use.
FileInfo fileInfo = new FileInfo(path);
string name = fileInfo .Name;
int startingNumber = Convert.ToInt32(name.Substring(0,3));
where path is the full path of the file your using

How to find country from ip adress?

I am using this API to find the country of a user. I am able to find the country on a web page in XML format. Here you can see XML file example. But the problem is i can not read this XML in my c# code. Here is my code
string UserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
string ApiKey = "5d3d0cdbc95df34b9db4a7b4fb754e738bce4ac914ca8909ace8d3ece39cee3b";
string Url = "http://api.ipinfodb.com/v3/ip-country/?key=" + ApiKey + "&ip=" + UserIP;
XDocument xml = XDocument.Load(Url);
But this code returns following exception on loading the xml.
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
Please describe the exact method to read this XML.
I'll say that it isn't an XML but simply a string subdivided by ;:
By giving an impossible IP address we can see that it's so composed:
OK;;74.125.45.100;US;UNITED STATES
ERROR;Invalid IP address.;127.0.0.1.1;;
OK/ERROR
If ERROR, complete ERROR message
IP Address
Abbreviation of country
Country name
This code should do:
string userIP = "127.0.0.1";
string apiKey = "5d3d0cdbc95df34b9db4a7b4fb754e738bce4ac914ca8909ace8d3ece39cee3b";
string url = "http://api.ipinfodb.com/v3/ip-country/?key=" + apiKey + "&ip=" + userIP;
WebRequest request = WebRequest.Create(url);
using (var response = (HttpWebResponse)request.GetResponse())
{
// We try to use the "correct" charset
Encoding encoding = response.CharacterSet != null ? Encoding.GetEncoding(response.CharacterSet) : null;
using (var sr = encoding != null ? new StreamReader(response.GetResponseStream(), encoding) :
new StreamReader(response.GetResponseStream(), true))
{
var response2 = sr.ReadToEnd();
var parts = response2.Split(';');
if (parts.Length != 5)
{
throw new Exception();
}
string okError = parts[0];
string message = parts[1];
string ip = parts[2];
string code = parts[3];
string country = parts[4];
}
}
Here is what I'd do:
Using a HTTP transfer, query the site, and buffer the results
Convert it into a String
Use a splitting algorithm on it to make into an array.
Check if the 0th element of the array equals 'OK'
If not, bail out.
If so, check the third and fourth elements for country code, and country name respectively.
from the IP Location XML API Documentation: API Parameter format, required = false, default = raw, values = raw, xml, json. so I have tested it and string Url = "http://api.ipinfodb.com/v3/ip-country/?key=" + ApiKey + "&ip=" + UserIP + "&format=xml" gives a parsable xml result.

Categories