|
There are many ways you could accomplish this. You could use a regular expression (a sledgehammer to crack a nut). You could use string.LastIndexOf to find the last / and then do a Substring based off this, or you could do a string.Split and take the last item out of the array.
|
|
|
|
|
Use System.IO.Path.GetFileName("http://localhost:99/test/TestHome.aspx")
|
|
|
|
|
Good one! My first impression was "that's a URL, not a path. It'll never work." Then I tried it.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
GetFileName is a good choice, but it does have the limitation of that it is actually doing more than is necessary to solve this particular problem. Why is that you ask? Well, when we deconstruct the method, it actually does the following:
public static string GetFileName(string path)
{
if (path != null)
{
CheckInvalidPathChars(path);
int length = path.Length;
int num2 = length;
while (--num2 >= 0)
{
char ch = path[num2];
if (((ch == DirectorySeparatorChar) || (ch == AltDirectorySeparatorChar)) || (ch == VolumeSeparatorChar))
{
return path.Substring(num2 + 1, (length - num2) - 1);
}
}
}
return path;
} As you can see, right at the top of the method, it's calling CheckInvalidPathChars to determine whether or not the path is valid. However, this is just a minor point - yours is a perfectly good solution, hence my 5.
|
|
|
|
|
Don't worry about anything, single one line code will solve problem,
i have complete program for you:
String s = "http://localhost:99/test/TestHome.aspx";
s = Path.GetFileName(s.Replace("/", "\\"));
Console.WriteLine(s);
Console.ReadKey();
|
|
|
|
|
|
GetFileName is a crap choice, doesn't sound very portable at all and there is a class dedicated to Uri handling already :P
Votes of 1 all around on this
Surely you'd do
var result = new Uri("http://foo.com/foo.aspx").Segments.Last();
|
|
|
|
|
So, an even less efficient method to retrieve this then. As the OP hasn't stated they are doing anything else Uri, it's kind of a waste when you end up with this method
public string[] Segments
{
get
{
int index;
if (this.IsNotAbsoluteUri)
{
throw new InvalidOperationException(SR.GetString("net_uri_NotAbsolute"));
}
string[] strArray = null;
if (strArray != null)
{
return strArray;
}
string privateAbsolutePath = this.PrivateAbsolutePath;
if (privateAbsolutePath.Length == 0)
{
return new string[0];
}
ArrayList list = new ArrayList();
for (int i = 0; i < privateAbsolutePath.Length; i = index + 1)
{
index = privateAbsolutePath.IndexOf('/', i);
if (index == -1)
{
index = privateAbsolutePath.Length - 1;
}
list.Add(privateAbsolutePath.Substring(i, (index - i) + 1));
}
return (string[]) list.ToArray(typeof(string));
}
} I'll be nice though. I won't award you a 1.
|
|
|
|
|
Path.GetFileName() is prefectly reasonable given the problem as described.
It describes the (apparent) intent, and so adds to the clarity of the code, which increases maintainability.
It is not necssarily the most efficient solution, but if done in response to user interaction, is probably plenty fast to not cause a performance issue.
|
|
|
|
|
A combination of the substring and IndexOf functions should be enough.
String.Split is the other neat option.
|
|
|
|
|
You could always use the System.Uri [^] class, which can provide much more information for you.
|
|
|
|
|
I'd use LastIndexOf and Substring, unless you actually want the extra validation of a path or URL parsing function.
|
|
|
|
|
Hello,
Try this:
Uri myUrl = new Uri("http://localhost:99/test/TestHome.aspx");
string pagina = myUrl.Segments.Last();
att.
Ernesto Fazolo
modified 22-Jun-12 13:09pm.
|
|
|
|
|
How can i create a tool with an windows media player and a playback device choiser like this..
http://www.abload.de/img/unbenannt-290mk6.png
(this is from teamspeak 3)
I got already a media player in my program and can play tracks with it, but the sound come all the time over my headset. I want to set that my program sound goes over the speaker. Someone know how to make this release?
Thanks for your help and sorry for my english ![Big Grin | :-D](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_biggrin.gif)
modified 2-Jul-12 8:04am.
|
|
|
|
|
here is the problem which I come across with:
I want to get process name ,application name and it's window title name
with the code below
listView1.Columns.Add("CurrentProcess", 2000);
listView1.Columns.Add("CurrentProcessPID", 500);
listView1.Columns.Add("CurrentApplicationname", 2000);
listView1.Items.Insert(1,Process.GetCurrentProcess().ToString());
listView1.Items.Insert(2,Process.GetCurrentProcess().MainWindowTitle);
listView1.Items.Insert(3,Process.GetCurrentProcess().Id.ToString());
but when I run my code I get this result
system.diagnostics.process(currentApplication on ByPId.vshost) form1
I enter this code to run in a timer tick so I use other application in the middle of this running code but I just get the result above.
I would appreciate any commend
|
|
|
|
|
whenever your app does something, it IS the current process. Therefore, GetCurrentProcess() isn't going to help you getting information about other processes.
The Process class does hold what you need, however, be aware there are a number of system processes you are not allowed to get information about (You would get some kind of access violation when you try). An alternative route would be using WMI, probably with the exact same limitations.
|
|
|
|
|
thanks a lot;
I would like to write an application to run at the back ground( I mean do such thing except for itself);
do you recommend any thing?
|
|
|
|
|
I second what Luc said above.
Just as a hint, if you need information for the processes currently running on the local system, you could use the Process.GetProcesses static method.
But I have to ask... What's wrong with Windows Task Manager?
2A
|
|
|
|
|
thanks for your comment;
I need it through a program ..
what I mean here is currently running application but not this application;
|
|
|
|
|
Well, an application is just that - a process. If you mean you need the processes that Task Managers displays in the Applications tab - another hint - that's a process that has a window , and if it has a window it should have a window handle .
Read about the Process class on MSDN, see what kind of properties and methods it has - the answer is all there (and here actually), so it's up to you to do the rest.
2A
|
|
|
|
|
Hi
I'm experimenting with SQLite. The program complies and runs fine, except I can't see the actual stored ID's in my file1.db. Am I looking in the wrong place or doing something wrong here? I would appreciate any feedback. I basically store random text from texbox into file1.db
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SQLiteConnection sqlcon;
private SQLiteCommand sqlcmd;
private SQLiteDataAdapter dataBase;
private DataSet dataSet = new DataSet();
private DataTable dataTable = new DataTable();
public Form1()
{
InitializeComponent();
createDataBase();
createDataTable();
}
private void createDataBase()
{
dataBase = new SQLiteDataAdapter();
}
private void createDataTable()
{
dataTable = new DataTable();
}
private void ExecuteQuery(string txtQuery)
{
using (SQLiteConnection sqlcon = new SQLiteConnection("Data Source=file1.db;Version=3;New=False;Compress=True;"))
{
using (SQLiteCommand sqlcmd = sqlcon.CreateCommand())
{
sqlcon.Open();
sqlcmd.CommandText = "create table records ( ID varchar(255))";
sqlcmd.CommandText = txtQuery;
sqlcmd.ExecuteNonQuery();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string txtQuery = "INSERT INTO RECORDS (ID) VALUES ('" + textBox1.Text + "')";
ExecuteQuery(txtQuery);
}
}
}
Thanks a lot
|
|
|
|
|
Are you looking at the correct database? You didn't specify a path, so it'll take the database that's in it's startup-path (probably the debug-folder of your app).
How are you "viewing" the data?
Bastard Programmer from Hell
|
|
|
|
|
Hi hope someone can help me with a bit of coding please.
I have on one form a text box in which a player should type their name as player 1, and on the 2nd form have that person's name show up in another text box.
Getting desparate here!
|
|
|
|
|
Hint do a search using passing data from 1 form to another , either in the forum or in the articles and you will find many answers to this question.
You might also try Google or any of the search engines.
I suggest you get a book on basic winforms programming and work through it. Learning from forum post is going to be very difficult!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Exactly how you do it depends on how your whole system works, but the first thing to do is to create a property in the Player Name form - call it PlayerName - and have it retrieve the value of the text box.
Now, either you need to use ShowDialog to display the PlayerName form in which case you retrieve the property immediately after showing the form (as ShowDialog stops execution until the form is closed). This is much like the way you do an OpenFileDialog:
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string lines = File.ReadAllLines(ofd.FileName);
...
The other way to do it is to create an event in the PlayerName form which the main form handles. When the event is signalled by the PlayerName form, the main form reads the property and acts on it immediately. This is similar to the way a search box works: when you type a character in the box, it immediately affects a list elsewhere.
Other than that, I can only echo Mycroft: Get a book, and work your way through it, doing all the exercises. Learning by random questions is a bad idea, it means you miss so much basic information and principles which makes it hard to work out what is going on later. You wouldn't learn to drive by jumping straight into a F1 race, would you?
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|