|
There are quite a few ways to do this, it will depend on what file information you have available.
String: Use String.LastIndexOf to find the final '\' character, and String.Substring to extract just the file name. Or just use Path.GetFileName(string s) to extract it from any string.
FileInfo: Use FileInfo.Name to extract just the file name.
There are probably others, but these should cover most cases.
All those who believe in psycho kinesis, raise my hand.
My 's gonna unleash hell on your ass. tastic!
|
|
|
|
|
Read up on the Path class; it holds everything you could want about file path strings.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
That is the correct answer.
|
|
|
|
|
You can do it one of two ways:
string[] pathParts = myPath.Split('\\');
and you'll end up with an array of strings that represent the various components.
Or, you can use the various System.Path.IO.GetXXXXX methods.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi all,
I am having difficulties formatting the output of the following writeline statement.
Console.WriteLine("CYLINDER1:\n RADIUS:"+ t1.Rad+"\n LENGTH:"+ t1.Len+"\n AREA:"+ t1.findArea()+"\n VOLUME:" + t1.findVolume());
Both radius and length are of type double, and both methods, findArea() and findVolume(), are defined as follows:
public double findArea()
{
return 2*(Math.PI * radius * radius) + 2*(Math.PI * radius * length);
}
public double findVolume()
{
return Math.PI *(radius * radius *length);
}
I would like the outputs of Area and Volume to have 4 numbers behind the decimal place. Please take a look and see what I'd have to change to make it output what I want. Thank you in advance for your help.
|
|
|
|
|
I was not sure you meant 4 numbers 'before' or 'after' the number when you mentioned 'behind'.
To set 4 numbers before the decimal use -
Console.WriteLine("{0:D4}", t1.findArea());
To set 4 numbers after the decimal use -
Console.WriteLine("{0:N4}", t1.findArea());
|
|
|
|
|
Hi Abhinav S,
Thanks for responding however, I already thought about doing what you have suggested prior to posting this question. The reason I did not go that route is because if I had decided to use placeholders to hold the incoming data, I'd have to have a writeLine statement for every variable within my original write statement. As you can see in my post I did not use any placeholders at all in my writeline statement. All I used are the string literals for AREA and VOLUME followed by t1.findArea() and t1.findVolume() etc. Is there any way I can still have the outputs I'm looking for without having to resort to having a writeline statement for each variable within my original writeline statement? Thanks again for your help.
|
|
|
|
|
Console.WriteLine("CYLINDER1:\n RADIUS:"+ t1.Rad+"\n LENGTH:"+ t1.Len+"\n AREA:"+ t1.findArea().tostring(".0000")+"\n VOLUME:" + t1.findVolume().tostring(".0000"));
is that what you want?
|
|
|
|
|
Hi sanforjackass,
Thankyou so much that is exactly what I wanted.
|
|
|
|
|
|
Hi everyone,
Thank you, you all have really helped expand my knowledge on string formatting.
|
|
|
|
|
can anyone help me to get window using process thread
SUCIL
|
|
|
|
|
You can use this to get an instance to the current thread:
Thread _currentThread = Thread.CurrentThread
Cheer's,
Alex Manolescu
|
|
|
|
|
Are you looking for the main form in your own application ?
A main window in a different application?
Is the other process a .NET application?
Please explain
|
|
|
|
|
I assume that you want to get the window handle from a thread id. To do that, I would:
- Pass the thread id to GetProcessIdOfThread() to obtain a process id
- Because a process can open multiple windows, you need to return a collection. Iterate every window, getting their process ids. If it matches the id from 1., then add the window handle to the collection
|
|
|
|
|
i m try to save a file but it does not work
can anybody suggest me some good tectics using C# codings
|
|
|
|
|
public void copyBookDatabase()
{
string filename ="mydb.mdb";
string destfileName = filename;
string sourcePath = Environment.CurrentDirectory;
string targetPath = @"C:\DB_Backup\";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, destfileName);
System.IO.File.Copy(sourceFile, destFile, true);
}
hope this helps
|
|
|
|
|
tanweer akhtar wrote: i m try to save a file but it does not work
Are you getting an error?
If yes, post the error here and someone might be able to help you.
|
|
|
|
|
hello to all
i m a student
i m interested in C#
i have a problem now
i m aunable to use the '\' or "\" in my program how i can use it in C#
can any body help me
thank lords!!!!!!!!!
best reguards >>>>>>
|
|
|
|
|
|
I think the type of '\' is character,the type of "\" is string
April
Comm100 - Leading Live Chat Software Provider
modified 27-May-14 8:51am.
|
|
|
|
|
Basically lets say you have 3 items in an array[]
is there a way to do something like this
for (int i=0; i< array[].length; i++)
{
string Name + i = blahblah.tostring();
basically so I would have 3 strings
Name1
Name2
and Name3
|
|
|
|
|
Not that I konw of.
You could use a dictionary<string, string> where the key is the "name
of the variable".
something like:
Dictionary<string, string> blah = new Dictionary<string, string>();
for (int i = 0; i < array[].length; i++)
{
blah.Add("Name" + i.ToString(), "blablablah");
}
|
|
|
|
|
If they are fields/properties instead of local variables you could do something like that with reflection, but you really shouldn't.
|
|
|
|
|
Well what I was thinking is since my current code is giving me an issue with resetting the dataset inside a foreach loop I was gonna do the following to populate a tree view.
string[] AuthorNames = new string[bookdbDataSet.Tables[0].Rows.Count];
int row=0;
foreach (DataRow Row in bookdbDataSet.Tables[0].Rows)
{
AuthorNames[row] = Row["AuthorFirst"].ToString() + " " + Row["AuthorLast"].ToString();
TreeNode [DYNAMIC NAME HERE] = new TreeNode(Row["AuthorLast"].ToString() + ',' + Row["AuthorFirst"].ToString());
rootNode.Nodes.Add(tn);
row++;
}
for (int counter = 0; counter < AuthorNames.Length; counter++)
{
string[] splitarray = null;
string Aname = AuthorName[i].tostring();
splitarray = Aname.split(" ");
bookListTableAdapter.FillByFirstandLast(bookdbDataSet.BookList, SplitArray[1].ToString(), SplitArray[0].ToString());
foreach (DataRow ChildRow in bookdbDataSet.Tables[0].Rows)
{
TreeNode child = [DYNAMIC NAME HERE].Nodes.Add(ChildRow["Title"].tostring());
}
[DYNAMIC NAME HERE].nodes.add(child);
}
So basically I get all the unique author names first and put them in an array
Then I fill the dataset one at a time for each author and then run a foreach to get the titles and then I assign them to the node of the Rootnode's dynamic name
|
|
|
|