I have got a strange template whose extension is ".docx.amp" . Now i want to get fileName. i.e "template". Path.GetFileNameWithoutExtension() fails as it returns template.docx. Is there any other built in mechanism or i will have to go the ugle string comparison/split way. Please suggest and give some workaround
Nope, you will have to use some kind of string split, eg:
var nameWithoutExtension = filename.Split('.')[0];
Related
I will have always an string like this:
"/FirstWord/ImportantWord/ThirdWord"
How can I extract the ImportantWord? Words can contain at most one space and they are separated by forward slashlike I put above, for example:
"/Folder/Second Folder/Content"
"/Main folder/Important/Other Content"
I always want to get the second word(Second Folder and Important considering above examples)
how about this:
string ImportantWord = path.Split('/')[2]; // Index 2 will give the required word
I hope you need not to use the String.Split option either with specific characters or with some regular expressions. Since the inputs are well qualified paths to a directory you can use Directory.GetParent method of the System.IO.Directory class, which will give you the parent Directory as DirectoryInfo. From that you can take the Name of Directory which will be the required text.
You can use like this :
string pathFirst = "/Folder/Second Folder/Content";
string pathSecond = "/Main folder/Important/Other Content";
string reqWord1 = Directory.GetParent(pathFirst ).Name; // will give you Second Folder
string reqWord2 = Directory.GetParent(pathSecond).Name; // will give you Important
Additional note: The method Directory.GetParent can be nested if you need to get a name in another level.
Also you may try this:
var stringValue = "/FirstWord/ImportantWord/ThirdWord";
var item = stringValue.Split('/').Skip(2).First(); //item: ImportantWord
There are several ways to solve this. The simplest one is using String.split
Char delimiter = '/';
String[] substrings = value.Split(delimiter);
String secondWord = substrings[1];
(you may want to do some input check to make sure the input is in the right format or else you will get some exception)
Other way is using regex when the pattern is simple /
If you are sure this is a path you can use other answer mention here
This is probably a simple question, I'm writing a WinForms C# application in VS 2012. I was wondering if there a way to add an extension such as .csv to some writing in a textbox. Say the user wrote in C:\Users\Desktop\filename but left out the .csv part of the path. Is there any way to add the .csv after an execute button is clicked?
Any help would be much appreciated.
You can use Path.ChangeExtension.
// Nota bene: Path.ChangeExtension does not change textBox1.Text directly (or any
// argument given), you MUST use the result if you care about it.
string newPath = Path.ChangeExtension(textBox1.Text, "csv");
The period is optional, and the filename component need not include an extension.
As a future reference, if you can think of something you need to do with a path to a file or a directory...it exists in System.IO.Path. Rare for there not to be support for a common task in that class.
If you do not want to change a valid extension in the string, you could do it like this instead:
// first test for an extension
if(!Path.HasExtension(textBox1.Text.Trim()))
{
// then add on '.csv' if one does not exist
string path = Path.ChangeExtension(textBox1.Text.Trim(), ".csv");
// ... use path ...
}
I have a url:
http://www.abc.com?refurl=/english/info/test.aspx?form=1&h=test&s=AB
If I use
Request.QueryString["refurl"
but gives me
/english/info/test.aspx?form=1
instead I need full url
/english/info/test.aspx?form=1&h=test&s=AB
Fix the problem, and the problem is that you place a full url as parameter refurl with out encoding it.
So where you create that url string use the UrlEncode() function, eg:
"http://www.abc.com?refurl=" + Server.UrlEncode(ReturnUrlParam)
where
ReturnUrlParam="/english/info/test.aspx?form=1&h=test&s=AB";
For that particular case you shouldn't use QueryString, (since your query string contains three parameters,) instead use Uri class, and Uri.Query will give you the required result.
Uri uri = new Uri(#"http://www.abc.com?refurl=/english/info/test.aspx?form=1&h=test&s=AB");
string query = uri.Query;
Which will give you :
?refurl=/english/info/test.aspx?form=1&h=test&s=AB
Later you can remove ?refurl= to get the desired output.
I am pretty sure there is no direct way in the framework for your particular requirement, you have to implement that in your code and that too with string operations.
I had similar situation some time ago.
I solved it by encoding refurl value.
now my url looks similar to that one:
http://www.abc.com?refurl=adsf45a4sdf8sf18as4f6as4fd
I have created 2 methods:
public string encode(string);
public string decode(string);
Before redirect or where you have your link, you simple encode the link and where you are reading it, decode before use:
Response.redirect(String.Format("http://www.abc.com?refurl={0}", encode(/english/info/test.aspx?form=1&h=test&s=AB));
And in the page that you are using refurl:
$refUrl = Request.QueryString["refurl"];
$refUrl = decode($refUrl);
EDIT:
encode/decode methods I actually have as extension methods, then for every string I can simply use string.encode() or string.decode().
you should replace the & with &.
i want get extension file of url .
for example get extension http://www.iranfairco.com/download-file/02912bb889cb24.
if extension this file is gif.
EDIT:
for example this url "http://www.online-convert.com/result/d8b423c3cbc05000cc52ce7015873e72"
Please help me how can get extension of this url?
Why does Path.GetExtension not work for you?
EDIT
Ah, so your url doesn't specify the extension you are after. That means anything could be behind it.
Just by inspecting the string value of the url, you can't see what will behind it. Your (second) example point (ultimately) to a .gif file, but it could also have been a .jpg, .doc (or even .exe).
You will need to do a webrequest to see what you get back. Usually you could try a HEAD to get just the headers (and inspect the content type), but I don't think that will work here. Try Fiddler to see what you get back, then you can refine your question.
Check out here:
http://www.java2s.com/Code/CSharp/Network/GetHTTPResponseheaders.htm
You will be interested in the response.ContentType which should give you something like "image/gif" for a gif image
try this
string path = #"http://www.iranfairco.com/download-file/02912bb889cb24.gif";
string e = Path.GetExtension(path );
This URL appears to perhaps be linking to a page which will have a download for a file.
The 02912bb889cb24 is perhaps a database reference to that file.
Therefore without access to any API for this system, I don't think its possible at all to work out the file extension for the file accessed via this URL
You can just split the string on char '.' and get the last string in the returned array..
I tried using Path.GetDirectoryName() but it doesn't work.
What I'm trying to get is from /home/nubela/test/some_folder , I wanna get "some_folder"
How can I do this? The method should work for both Windows/Linux (Mono)
Thanks!
Use Path.GetFileName instead? These functions work just on the string you provide and don't care if it's a directory or a file path.
If you have the path as a string already you can use this method to extract the lowest level directory:
String dir
= yourPath.Substring(
yourPath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
Since this code uses Path.DirectorySeparatorChar it is platform independent.
My first idea would be to use System.IO.Path.GetDirectoryName, too. But you can try a regular expression to get the final substring of your string. Here is an answer in StackOverflow, using regular expressiones, that answers this.