|
Thanks to everyone who responded to my post! I got a lot of good opinions and dialogs going, and even some reasons why particular language design choices were made (although I still disagree with them). Perhaps the title of the post should have been, "Some of the features I miss from C++", as I still plan on using C# for my projects that I do in my free time, even when someone doesn't force me to. There are many great features of C# to take advantage of, and despite the few drawbacks (in my opinion, pretty significant drawbacks), it really is a great language. Thanks again everyone!
Jeff
|
|
|
|
|
hi,
How can I get all names of files which have extension of mp3 from filesystem (c:\ part of computer for example). I can get only the names of files with extension of mp3 in only one folder by using beloved code. By this code,I can't get, if there are mp3 files in subfolders, but I want to get all mp3 files' names under c:\ part of computer.
private void button1_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\");
System.IO.FileSystemInfo[] fi = di.GetFileSystemInfos();
foreach (System.IO.FileSystemInfo f in fi)
{
if (f.Extension==".mp3")
{
richTextBox1.Text += f.Name;
richTextBox1.Text += Environment.NewLine;
}
}
richTextBox1.SaveFile(@"C:\mp3ler.xls",RichTextBoxStreamType.PlainText);
}
|
|
|
|
|
Recursion:
void GetMP3(string Path)
{
...
foreach(FileSystemInfo f in fi) {
if(f.Extension == ".mp3") {
richTextBox1.Text += f.Name;
richTextBox1.Text += Environment.NewLine;
}
}
foreach(DirectoryInfo d in di) {
GetMP3(d.FullName);
}
...
}
The function will keep calling itself for all subdirectories within a folder, quite clever when you think about it.
That code might not work, but you get the idea.
You may also need to call Application.DoEvents() somewhere too, so that your window doesn't lock up while you trawl through the compy looking for mp3's
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
Actually, Application.DoEvents isn't really the best way to handle that. You're absolutely right that the application should yield to the UI thread so the application remains responsive, but Application.DoEvents forces the application message pump to run and isn't recommended.
Ideally the code above should be performed on a separate thread from the main UI thread. There are many ways to do that including creating a new thread, using the ThreadPool or asynchronous execution of a delegate (delegate.BeginInvoke / EndInvoke). But one of the coolest ways to execute code off the UI thread is the Dispatcher class. You can use the Dispatcher to execute code with various priority levels and even postpone execution until the application is idle. Note that Dispatcher is new to .Net 3.0, so if you're tied to 1.x or 2.0 then you won't be able to use it. You can read about the Dispatcher here[^].
Unfortunately, once this code is running on a separate thread you still have to get back to the UI thread to update controls. If you have a listbox, for example, and you want to add a ListboxItem for each file you find you won't be able to do it from the worker thread. You can get around that using Control.Invoke, but there's an even easier way if you're in .Net 3.0 or later. That's the BackgroundWorker[^] component, which will help you in easily setting up the background thread to execute your code and even gives you a very easy way to communicate back to the UI thread. BackgroundWorker is certainly the easiest, but if it's too restrictive for what you're trying to do make sure you check out some of those other options.
Regards,
Jared Bienz
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Be sure to run the code from the prior post in a separate thread, or Windows will assume your program is "hung" for long directory searches (such as the entire C drive), and when you do a ctl-alt-del your program will appear in the list as not responding (if not in a separate thread).
Jeff
|
|
|
|
|
Thats why i added in to use Application.DoEvents() . It gives control to the main window to do what it needs to, process messages, redraw, etc.
Though i agree that you should probably put it in a separate thread, although that will make it a little more difficult to update your textbox, though it shouldn't be too much trouble.
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
hi,
Directory.GetFiles(string path, string searchPattern, SearchOption searchOption)
searches recursively if you ask for it.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
The overload of Directory.GetFiles that takes a SearchOption is only available in .Net 3.0 or later, though I agree it's the best option.
Note that this can still take a long time and should still potentially be run on a separate thread.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Jared Bienz [MSFT] wrote: The overload of Directory.GetFiles that takes a SearchOption is only available in .Net 3.0 or later
I have it running on 2.0, it runs fine except it fails searching at the device level
(such as path="C:\") due to an access denied on "C:\System Volume Information\".
Jared Bienz [MSFT] wrote: should still potentially be run on a separate thread
I fully agree; I would suggest to do allmost all multi-file operations on a separate thread,
and in a try-catch construct.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thanks for your replies,
Is there an easier way for this?
|
|
|
|
|
Hello, I am trying to create a program that reads in 3 numbers and averages them for my Programming 1 class. I am trying to use method call statements in order to do this. I got it all finished but when I built it I got three "Identifier Expected" errors on line 26, and I don't understand what that means. (I'm rather new) Here is what I've got for coding...I'm not sure if there is anything else I need to change about it other than the 3 errors, either.
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace Program10<br />
{<br />
class Program<br />
{<br />
static void GetNums(out double num1, out double num2, out double num3)<br />
{<br />
Console.Write("Enter first number: ");<br />
num1 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter second number: ");<br />
num2 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter third number: ");<br />
num3 = double.Parse(Console.ReadLine());<br />
}<br />
static void CalcAvg(out double num1, out double num2, out double num3)<br />
{<br />
double avgNum;<br />
avgNum = num1 + num2 + num3;<br />
avgNum = avgNum / 3;<br />
<br />
return (double) avgNum;<br />
}<br />
static double DspAvg(double avgNum, out num1, out num2, out num3)<br />
{<br />
Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum);<br />
}<br />
<br />
static void Main()<br />
{<br />
double firstNum, secNum, thirdNum, avgNum;<br />
DspAvg(firstNum, secNum, thirdNum);<br />
GetNums(out firstNum, out secNum, out thirdNum);<br />
CalcAvg(avgNum);<br />
}<br />
<br />
}
Thanks in advance for your help!
[Ross Murtle]
[Computer Science Major - Iowa Central Community College/Iowa State University]
|
|
|
|
|
and which of these is line 26?
don't tell us you are running Visual Studio with line numbering disabled!
and please use < PRE > tags for code snippets.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
static double DspAvg(double avgNum, out num1, out num2, out num3)
is line 26. Sorry for the confusion.
|
|
|
|
|
num1/2/3 are missing their types, something like int
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
First off, Welcome to Programming C#! It's my favorite language.
There are a handfull of issues, but at least you gave it the good old college try.
Let's get started: You were missing a close curly brace at the end, though that could have simply not made it to your clipboard.
Next, (and this was the 'Identifier Expected' issue) in DspAvg you didn't define the data type for num1 num2 or num3. You must specify that they're double.
After that, in DspAvg you also can't mark those parameters as 'out' because DspAvg never assigns them a value -- it just prints them on the screen. The 'out' keyword signifies that a value will be assigned to the parameter before the method returns.
The same thing happened in CalcAvg. No need to mark them as 'out' since they're not being assigned in the method.
In CalcAvg you explicitly cast avgNum to a double, which isn't necessary since avgNum is defined as a double at the beginning of the method. It'll still compile, but it's unecessary.
Now, here it get's a little interesting. DspAgv is marked to return a double, but no value is returned (and one shouldn't be because you're just displaying the results). However, your CalcAvg method doesn't return a value and of course it really should.
In Main it looks like you were trying to use DspAvg to show the collected values, but DspAvg requires a fourth parameter (the calculated average). You could either remove that parameter or not use DspAvg until you've done the calculation.
In Main, when you call CalcAvg you'll need to store the return value.
You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio).
Here are the changes, hope it works for you. Now please don't expect another free homework assignment, but DO enjoy learning C#.
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace Program10<br />
{<br />
class Program<br />
{<br />
static void GetNums(out double num1, out double num2, out double num3)<br />
{<br />
Console.Write("Enter first number: ");<br />
num1 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter second number: ");<br />
num2 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter third number: ");<br />
num3 = double.Parse(Console.ReadLine());<br />
}<br />
<br />
static double CalcAvg(double num1, double num2, double num3)<br />
{<br />
double avgNum;<br />
avgNum = num1 + num2 + num3;<br />
avgNum = avgNum / 3;<br />
<br />
return avgNum;<br />
}<br />
<br />
static void DspAvg(double avgNum, double num1, double num2, double num3)<br />
{<br />
Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum);<br />
}<br />
<br />
static void Main()<br />
{<br />
double firstNum, secNum, thirdNum;<br />
GetNums(out firstNum, out secNum, out thirdNum);<br />
double avgNum = CalcAvg(firstNum, secNum, thirdNum);<br />
DspAvg(avgNum, firstNum, secNum, thirdNum);<br />
Console.WriteLine("Press Enter to end the application.");<br />
Console.ReadLine();<br />
}<br />
<br />
}<br />
}<br />
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
|
|
|
|
|
Thank you SO MUCH for your help. I have learned from my mistakes and the fact that you took time out of your day to help a measly little college freshman like myself is greatly appreciated!
Thanks again!
|
|
|
|
|
No problem at all. The secret to becoming a successful developer is being resourceful and not giving up. Glad I could help.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Jared Bienz [MSFT] wrote: The secret to becoming a successful developer is being resourceful and not giving up
Its not. The secret is caffeine and pizza. Plenty of it.
|
|
|
|
|
Caffeine and Pizza inherit from Resource, don't they?
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: Caffeine and Pizza inherit from Resource, don't they?
They do. And best of all, they are Disposable - although the Garbage Collection is a bit unpredictable.;)
|
|
|
|
|
Jared Bienz [MSFT] wrote: You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio).
I actually find that:
Console.ReadKey(true);
Is much better, as soon as you press any key (apart from a select few, NumLock, for example) it will close. The 'true' part means that it will not send the input to the buffer, so you won't actualy see the letter or number you pressed on the screen.
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
Ah, much better. Thanks for pointing that out. I'm going to use that from now on in my SequentialWorkflow console demos!
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Please help me to find a good and simple Remoting tutorial.
I have 6 mnth exp in c# (So now you know my level of understanding).
Thanks in advance.
v
|
|
|
|
|
Try this[^]. You can find it easily enough by using Google[^]
|
|
|
|
|
Petes article above is a great place to start. You might also want to dive into the .Net Remoting Overview[^] section on MSDN. Specifically there's a good article titled Building a Basic .NET Framework Remoting Application[^]
I'd also like to make sure you're aware of Windows Communication Foundation (WCF). You may have a business reason for using Remoting so I wanted to provide those resources, but if this is a hobby project or you’re starting new product development you really should consider using WCF instead. .Net Remoting will be around for a long time yet, but Microsoft is no longer developing that technology and WCF is the recommended platform for all new communications development.
WCF will still let you do the same things that Remoting will (cross-process communication, fast binary encoding, singletons, serialization, etc.) but it will also let you do so much more. A WCF service can be hosted in-process like a Remoting service can, but it can also be hosted, activated and load balanced on an IIS server. WCF allows the same service instance to be contacted over a binary TCP channel, a SOAP web service channel, a named pipe or even a message queue -- all without changing a single line of code. How a service behaves, how security is applied and how a service communicates can all be described in code or alternatively in an XML configuration file without needing to recompile the application. You can even use WCF to write P2P applications!
If you'd like to check out WCF I recommend you start with http://wcf.netfx3.com[^] or the Windows Communication Foundation[^] area on MSDN.
I hope you find what you're looking for, and happy coding!
Jared
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
|
|
|
|
|