I have filename like
ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg
How can I find this string ChrysanthemumThumbnail
You can use Path.GetFileNameWithoutExtension method to read the file name without extension and then you can use string.Contains to check if it contains your string.
string fileName = Path.GetFileNameWithoutExtension("ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg");
if(fileName.Contains("ChrysanthemumThumbnail"))
{
//name found
}
if you want to see if the fileName starts with your string then you can use string.StartsWith
if(fileName.StartsWith("ChrysanthemumThumbnail"))
{
//file name starts with ChrysanthemumThumbnail
}
(if you have the FileName already in a string then you don't need Path.GetFileNameWithoutExtension it is only useful if you are trying to extract the filename from a path)
Did you want something like:
string filename = "ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg"
bool result = filename.Contains("ChrysanthemumThumbnail");
if the ChrysanthemumThumbnail always at start of the filename then just use 'StartWith'
, this will have better over contains which checks whole string
"ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg".StartsWith("ChrysanthemumThumbnail")
and if it can be at any location than use contains as suggested by Habib above
"ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg".Contains ("ChrysanthemumThumbnail")
Related
I am developing an internal application which sends email to the users with the link to the training dcouments.
These documnets are placed in internal share drive, few of these documents have empty space in their names and thats causing the problem.
The path looks like \\Users\shared\Training\Database\Oracle\Docs\Oracle Database Admin.docx and i tried to replace empty space with %20 but still it doesn't work.. In the email link the path is trimmed to \\Users\shared\Training\Database\Oracle\Docs\Oracle
Public string GetMediaPath(int itemCode)
{
string path = _dbContext.TraningMedias.Where( s => s.ItemCode == itemCode).Select(a => a.Path).FirstOrDefault().ToString();
path.replace(" ", "%20");
return path;
}
I dont understand why the replace function is not working in this case.
Strings are immutable, and Replace returns a string, so try this:
path = path.Replace(" ", "%20");
To preserve the spaces in your link text, use an opening and closing chevron
Public string GetMediaPath(int itemCode)
{
string path = "<"+ _dbContext.TraningMedias.Where( s => s.ItemCode == itemCode).Select(a => a.Path).FirstOrDefault().ToString() + ">";
return path;
}
Try doing this:
The below code will remove all invalid filename characters from the path.
path =string.Concat(path.Split(Path.GetInvalidFileNameChars()));
Dont forget to include System.IO namespace.
Thanks
You can try url encode adn get rid off spaces and others speacial characters.
path= HttpUtility.UrlDecode(path);
Just convert the raw file path string to a proper URI, like this:
string fileUrl = new System.Uri("c:\\foo\\my document.docx").AbsoluteUri
which will give you this string:
"file:///c:/foo/my%20document.docx"
Look this
In your case:
path = Uri.EscapeUriString(path);
I want to find a file that has .pdf extension and I want to find with name too. For example I have filename = "Work.pdf". I want to write a method that
could find with name. Is there any builtin method for this?
string fileName = "Work.pdf"
string[] files = System.IO.Directory.GetFiles("C:\Files", "*.pdf");
// string myWorkPdfFile = files.Where() //search the files with fileName
You can try to pass fileName value be second parameter.
string fileName = "Work.pdf";
string[] files = System.IO.Directory.GetFiles(#"C:\Files", fileName);
i want to store the name of all file present in c:\test\ in a string array s[]
let there are files name a1.txt , a2.txt , a3.txt ... in c:\test
i want to store in s[0]= a1.txt s[1]= a2.txt and like that
i have used the code
s = Directory.GetFiles(#"c:\\test");
but it makes s[0]= c:\test\a1.txt
i dont want c:\test , i only want a1.txt
so is there any method to store only the file name but not the path of the file
i would also like to know if there is any method to remove some characters from each string of a string array
like cutting 5 characters from the beginning of each string of a string array this may also solve my problem.
Use GetFileName to extract file name from path. Like below
string[] s = Directory.GetFiles(#"c:\\test");
foreach (string filename in s)
{
string fname = Path.GetFileName(filename);
}
maybee duplicate from hare..
any way #Vasea gives us :
Directory.GetFiles(#"c:\test", "*", SearchOption.TopDirectoryOnly).Select(f => Path.GetFileName(f));
How to remove the some characters in a string ..
string s="testpage\information.xml"
I need only information.xml how to do that?
System.IO.Path may help you with this since the string contains a file path information. In your case, you may use Path.GetFileName(string path) to get the file name from a string.
Example
string s = #"testpage\information.xml";
string filename = Path.GetFileName(s);
//MessageBox.Show(filename);
Thanks,
I hope you find this helpful :)
Assuming the value that will be in s is always a file path, use the Path class to extract the file name
var filename = Path.GetFileName(s);
File path is of the form
aaaa\bbb\ccc\dddd\information.xml
To retrieve the last string, you can divide your string using the delimiter \
String path = #"aaaa\bbb\ccc\dddd\information.xml";
String a[] = path.Split('\\');
This will give String array as ["aaaa", "bbb", "ccc", "dddd", "information.xml"]
You can retrieve the filename as
String filename = a[a.Length-1];
If it is going to be a file path, then you can use the System.IO.Path class (MSDN) to extract the filename.
string s = "testpage\information.xml"
var filename = Path.GetFilename(s);
If it's always right of the backslash separator then you can use:
if (s.Contains(#"\"))
s= s.Substring(s.IndexOf(#"\") + 1);
Hope this is what you want:
var result=s.Substring(s.LastIndexOf(#"\") + 1);
If you are using file paths, see the Path.GetFileName Method
It will not check whether the file exists or not. So it will be faster.
s = Path.GetFileName(s);
If you need to check whether file exists, use File.Exists class.
Another way is to use String.Split() method
string[] arr = s.Split('\\');
if(arr.Length > 0)
{
s = arr[arr.Length - 1];
}
Another way is to use RegEx
s = Regex.Match(s, #"[^\\]*$").Value;
You can use the following line of codes to get file extension.
string filePath = #"D:\Test\information.xml";
string extention = Path.GetExtension(filePath);
If you need file name alone use,
string filePath = #"D:\Test\information.xml";
string filename = Path.GetFilename(filePath );
Use string.Replcae
string s = #"testpage\information.xml";
s = s.Replace(#"testpage\\",""); // replace 'testpage\' with empty string
You will get Output => s=information.xml
# is need only because you have backslash in your string
For further reading about STRING REPLACE
http://www.dotnetperls.com/replace
http://msdn.microsoft.com/en-us/library/system.string.replace.aspx
In C++ you can do something like this. Basically search for "/" or "\" from right to left of the path and crop the string starting from the first occurance of the delimiter:
string ExtractFileName(const string& strPathFileName)
{
size_t npos;
string strOutput = strPathFileName;
if(strPathFileName.rfind(L'/', npos) || strPathFileName.rfind(L'\\', npos))
{
strOutput = strPathFileName.substr(npos+1);
}
return strOutput;
}
Could you help me for finding the file name from the string. Now i have one string of content like "C:\xxxx\xxxx\xxxx\abc.pdf". But i want only the file name ie. abc.pdf. How it will get by using string functions?
Use Path.GetFileName:
string full = #"C:\xxxx\xxxx\xxxx\abc.pdf";
string file = Path.GetFileName(full);
Console.WriteLine(file); // abc.pdf
Note that this assumes the last part of the name is a file - it doesn't check. So if you gave it "C:\Windows\System32" it would claim a filename of System32, even though that's actually a directory. (Passing in "C:\Windows\System32\" would return an empty string, however.) You can use File.Exists to check that a file exists and is a file rather than a directory if that would help.
This method also doesn't check that all the other elements in the directory hierarchy exist - so you could pass in "C:\foo\bar\baz.txt" and it would return baz.txt even if foo and bar don't exist.
Use the Path.GetFileName() Method
(Edited) sample from the MSDN-page:
string fileName = #"C:\xxxx\xxxx\xxxx\abc.pdf";
string path = #"C:\xxxx\xxxx\xxxx\";
string path2 = #"C:\xxxx\xxxx\xxxx";
string result;
result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result);
result = Path.GetFileName(path2);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path2, result);
This code produces output similar to the following:
GetFileName('C:\xxxx\xxxx\xxxx\abc.pdf') returns 'abc.pdf'
GetFileName('C:\xxxx\xxxx\xxxx\') returns ''
GetFileName('C:\xxxx\xxxx\xxxx') returns 'xxxx'
Sytem.IO.FileInfo is also quite cool:
In your case you can do
FileInfo fi = new FileInfo("C:\xxxx\xxxx\xxxx\abc.pdf");
string name = fi.Name; // it gives you abc.pdf
Then you can have several other pieces of information:
does the file really exist? fi.Exists gives you the answer
what's its extension? see fi.Extension
what's the name of its directory? see fi.Directory
etc.
Have a look at all the members of FileInfo you may find something interesting for your needs
Use the methods of System.IO.Path, especially
Path.GetFileName.
System.IO.Path.GetFilename(yourFilename)
will return the name of the file.
You could use System.IO.Path.GetFileNameWithoutExtension(string path):
System.IO.Path.GetFileNameWithoutExtension("C:\xxxx\xxxx\xxxx\abc.pdf")
And you would get back abc