File details from a string in C# - c#

I have a string in C#
String file="\\mserver-80\docs\somedoc.doc"
Now How do I get fileInfo from the above sting.
What I mean is,
I want to declare something like
FileInfo fInfo = new FileInfo(file);
fileExtn = fInfo.Extension;

You can also try
Path.GetExtension(file)

In C# the string should be
String file="\\\\mserver-80\\docs\\somedoc.doc";
You can also escacpe the string using the # character, which is a better alternative:
String file=#"\\mserver-80\docs\somedoc.doc";
other than that the code should work.

That code will work fine, using the FileInfo class.
Simply add
using System.IO;
However, note that the \ must be escaped as \\.
Instead, you should use an #"" string, like this:
String file = #"\\mserver-80\docs\somedoc.doc"

Related

How to pass one local string into the middle of another string as a variable? .NET C#

In the below example, how to change the local QNARESULTHERE.json file with the name of the qnaResult?
public async Task ITSupportIntent(IDialogContext context, LuisResult result)
{
var qnaResult = itKB.GetAnswer(result.Query);
if (qnaResult.StartsWith("CARD"))
{
var reply = context.MakeMessage();
try
{
string json = File.ReadAllText(HttpContext.Current.Request.MapPath("~\\AdaptiveCards\\QNARESULTHERE.json"));
Sorry this question is all over the place.
Context:
The actual variable i needed was the QnAAnswer i believe as referenced here: Integrate QnA Maker and LUIS to distribute your knowledge base
It's a little unclear exactly what value you are trying to get from the result, but assuming LuisResult has a FileName property on it, as an example, you can use string interpolation (available since C# 7), like this:
string relativePath = $"~\\AdaptiveCards\\{result.FileName}.json";
You may also find that the #-style verbatim string syntax works better here, since it means you don't have to escape backslashes:
string relativePath = $#"~\AdaptiveCards\{result.FileName}.json";
If you're using an older version of C#, you can also use string.Format or just plain old string concatenation:
string relativePath = string.Format(#"~\AdaptiveCards\{0}.json", result.FileName);
string relativePath = #"~\AdaptiveCards\" + result.FileName + ".json";
Whichever you choose, you will of course want to pass the resulting value along like you were doing.
string json = File.ReadAllText(HttpContext.Current.Request.MapPath(relativePath));
You can use string.Format() like so:
string json = File.ReadAllText(HttpContext.Current.Request.MapPath(string.Format(#"~\AdaptiveCards\{0}.json", qnaResult));
Above is assuming qnaResult is a string that contains the file name you want. If it's a class instance, then use the appropriate property of it that contains the file name.

how to remove last part of string in c#

I was trying to remove last part of a string but failed.Here string named D:\software\VS2012\newtext.txt and i want to trim last section of string so here newtext.txt . I should get D:\software\VS2012 but how to do it in c#.When i tried it is removing all the string that has '\'. Here is what i did in c#
string str = #"D:\softwares\VS2012\newtext.txt";
str= str.Remove(str.IndexOf('\\'));
Console.WriteLine(str);
There is a premade function for this in the framework
string str = #"D:\softwares\VS2012\newtext.txt";
string path = System.IO.Path.GetDirectoryName(str);
(Reference)
Note that your original code does not work because you are removing from the first backslash, not the last. Substitute this line to make your code work
str = str.Remove(str.LastIndexOf('\\'));
Try using System.IO.Path.GetDirectoryName(string):
string dirname= System.IO.Path.GetDirectoryName(#"D:\softwares\VS2012\newtext.txt");
For removing a known portion of a string you can simply use the Replace.
In your case:
str = str.Replace("\\newtext.txt", ""); //this will give you the same result of the System.IO.Path.GetDirectoryName already suggested by gmiley, but it's more in a string context as per your question
Though if you want to remove the last part of a string by the last encounterd known character then the suggested "LastIndexOff('\')" method already suggested along with the Remove.
If you want to use a delimiter method, so depending on the delimiter character but not on the string format (in your case path format) the LastIndexOff(char) is the best option.
Although you could also split the string into an array and then rejoin the array after removing the last element:
var delmimter = '\\';
var strAy = str.Split(char);
str = String.Join('\\', strAy.SkipLast(1).ToArray());
With this method you don't need to rely on the existence of the delimiter char in the string and the result is always without the delimiter char at the end.
Besides, you can easily create an extension with the delimiter as a parameter.
We should check the existance of the char also
string str = #"D:\softwares\VS2012\newtext.txt";
int rstr = str.LastIndexOf('\\');
if (rstr>0) str= str.Remove(rstr);
Console.WriteLine(str);

Why My string . Find is not avalible

I am using Visual studio 2010.
I need to replace some part of my string, thus I should find the specific charter in my string.
but .find is not available for me.
What should I do, is there any specific library for that?
string selected="ddddtttjjj";
selected.find("t");
C#, String.IndexOf
string str = "abcdefg";
var ix = str.IndexOf("d");
Console.WriteLine("Ix=" + ix);
// output
//Ix=3
http://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx
but, if you want to repleace a substring you can use. "String.Replace"
string str = "abcdefg";
str = str.Replace("d","xDx");
Console.WriteLine(str);
// ouput
// abcxDxefg
http://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx
note that String.Replace Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

How to remove end of string (fileName) using substring?

I know I must use Substring to remove, but I dont know how to do this. I need to remove end of string like this
from
"C:\\Users\\myname\\Pictures\\shoeImage.jpg"
to
"C:\\Users\\myname\\Pictures"
Use the methods of the System.IO.Path class instead, in specific GetDirectoryName.
You can use Path.GetDirectoryName method.
Returns the directory information for the specified path string.
Console.WriteLine(Path.GetDirectoryName("C:\\Users\\myname\\Pictures\\shoeImage.jpg"));
It returns this;
C:\Users\myname\Pictures
Here a DEMO.
With String.SubString method, you can use it like;
string path = "C:\\Users\\myname\\Pictures\\shoeImage.jpg";
Console.WriteLine(path.Substring(0, path.LastIndexOf(#"\")));
You should use FileInfo in such scenarios -
FileInfo info = new FileInfo("C:\\Users\\myname\\Pictures\\shoeImage.jpg");
string name = info.DirectoryName;
OR
Path.GetDirectoryName("C:\\Users\\myname\\Pictures\\shoeImage.jpg");
If you want to substring it:
var subString = yourString.SubString(0, yourString.LastIndexOf('\\'));

How can I add " character to a multi line string declaration in C#?

If I write something like this:
string s = #"...."......";
it doesn't work.
If I try this:
string s = #"...\".....";
it doesn't work either.
How can I add a " character to a multi line string declaration in C#?
Try this:
string s = #"..."".....";
The double character usage also works with the characters { and } when you're using string.Format and you want to include a literal instance of either rather than indicate a parameter argument, for example:
string jsString = string.Format(
"var jsonUrls = {{firstUrl: '{0}', secondUrl: '{1}'}};",
firstUrl,
secondUrl
);
string s = "...\"....."; should work
the # disables escapes so if you want to use \" then no # symbol
Personally i think you should go with
string s = string.format("{0}\"{1},"something","something else");
it makes it easier in the long run

Categories