where to put sample code?

M

mp

trying to study the sample code for findfile
I believe from previous response here that one still has to use the win api
FindFirstFile, FindNextFile to search for files matching some criteria.
so i searched on msdn and found this sample

url to sample code:
http://msdn.microsoft.com/en-us/library/3k3z0k7a(VS.71).aspx

first code snippet is as follows:
<quote>

The FindFile sample uses the following unmanaged function, shown with its
original function declaration:

a.. FindFirstFile exported from Kernel32.dll.


HANDLE FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData);

<end quote>

it doesn't tell this abject beginner where that code goes.
If i start a new c# project(in express), say a windows form type
application, where does that definition belong?

do I create a class? or do i put it in the form.cs? if so, where in the
class or form code would that and subsequent code snippets from that sample
go?

that's one problem with being such a beginner, even when I find info in help
it sometimes assumes basic knowlege that i don't have yet...
like how to turn the computer on...<g> ....ok, i do know that much but not
much more....
:)

thanks
mark'
 
A

Arne Vajhøj

trying to study the sample code for findfile
I believe from previous response here that one still has to use the win api
FindFirstFile, FindNextFile to search for files matching some criteria.
so i searched on msdn and found this sample

url to sample code:
http://msdn.microsoft.com/en-us/library/3k3z0k7a(VS.71).aspx

first code snippet is as follows:
<quote>

The FindFile sample uses the following unmanaged function, shown with its
original function declaration:

a.. FindFirstFile exported from Kernel32.dll.


HANDLE FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData);

<end quote>

it doesn't tell this abject beginner where that code goes.
If i start a new c# project(in express), say a windows form type
application, where does that definition belong?

do I create a class? or do i put it in the form.cs? if so, where in the
class or form code would that and subsequent code snippets from that sample
go?

that's one problem with being such a beginner, even when I find info in help
it sometimes assumes basic knowlege that i don't have yet...
like how to turn the computer on...<g> ....ok, i do know that much but not
much more....
:)

Eksempel:

C:

void system(char *cmd);

Brug fra C#:

using System;
using System.Runtime.InteropServices;

class MainClass
{
[DllImport("msvcrt.dll")]
public static extern int system(string cmd);
public static void Main(string[] args)
{
system("DIR");
}
}

Arne
 
T

Tom Shelton

mp pretended :
trying to study the sample code for findfile
I believe from previous response here that one still has to use the win api
FindFirstFile, FindNextFile to search for files matching some criteria.
so i searched on msdn and found this sample

Hmmm... I wonder what all those overloads of
System.IO.Directory.GetFiles are then:

From the documentation:

GetFiles(String) - Returns the names of files in the specified
directory.

GetFiles(String, String) - Returns the names of files in the specified
directory that match the specified search pattern.

GetFiles(String, String, SearchOption) - Returns the names of files in
the specified directory that match the specified search pattern, using
a value to determine whether to search subdirectories.

Of course, the main problem with getfiles is that it will enumerate all
of the files in a directory before it returns. Real pain if your are
dealing with large numbers of files.

If your targeting the .NET4.0 framework, they you are better off using
DirectoryInfo.EnumerateFiles. If your not then, well, I have an
example of rolling up similar functionality using FindFirstFile, etc
and C#3.0's extension method facility:

http://tom-shelton.net/index.php/20...api-to-efficiently-enumerate-the-file-system/

If nothing else, it will show you how to use the api's :)
 
M

mp

M

mp

mp said:
many thanks, I'll study that
mark
Tom,
thanks for the link to your project.
I must have done something wrong.
I dled the zip file and unzipped to a folder
went into C:\...\FileSystemEnumerator\FileSystemEnumeratorDemo
and dbl clicked FileSystemEnumeratorDemo.csproj
when c# express trys to open the proj I get a message that some vbproj
couldn't be opened by this version(express)
in the ide, once the project opens I have 3 missing items
Enumerator Test(unavailable)
WindowsApplication1(unavailable)
WindowsFormsApplication1(unavailable)
I don't see any vbproj files in the unzipped contents
but i see this in the .sln file

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "WindowsApplication1",
"WindowsApplication1\WindowsApplication1.vbproj",
"{19957612-E2A8-4344-B6BB-B93BAB4C5411}"
EndProject

what am I doing wrong?
thanks
mark
 
M

mp

mp said:
Tom,
thanks for the link to your project.
I must have done something wrong.
I dled the zip file and unzipped to a folder
went into C:\...\FileSystemEnumerator\FileSystemEnumeratorDemo
and dbl clicked FileSystemEnumeratorDemo.csproj
when c# express trys to open the proj I get a message that some vbproj
couldn't be opened by this version(express)
in the ide, once the project opens I have 3 missing items
Enumerator Test(unavailable)
WindowsApplication1(unavailable)
WindowsFormsApplication1(unavailable)
I don't see any vbproj files in the unzipped contents
but i see this in the .sln file

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "WindowsApplication1",
"WindowsApplication1\WindowsApplication1.vbproj",
"{19957612-E2A8-4344-B6BB-B93BAB4C5411}"
EndProject

what am I doing wrong?
thanks
mark

nevermind,
I just removed those missing projects, set the demo project to start up and
it now works.
thanks, that's just what I was looking for.
mark
 
M

mp

Tom Shelton said:
mp pretended :
snip

I have an
example of rolling up similar functionality using FindFirstFile, etc and
C#3.0's extension method facility:

http://tom-shelton.net/index.php/20...api-to-efficiently-enumerate-the-file-system/

If nothing else, it will show you how to use the api's :)

Tom,
thanks for that project.
Lots to study and learn from in there.
Meanwhile I'm messing with an alternate display, commenting out the
console.writeline and substituting a form with listview, to display the
filenames.
What I'm trying to figure out is why the console window is still opening,
even though i've commented out that code and find no other lines calling it
to open.
the modified code is as follows:
namespace FileSystemEnumeratorDemo
{
class Program
{
static void Main()
{
Form1 f = new Form1();
f.ShowDialog();
}

// static void Main ( string[] args )
// {
// string path = "C:\\0\\0info\\dotnetinfo";
// DirectoryInfo root = new DirectoryInfo ( path );

// var fileQuery = from fileInfo in root.EnumerateFiles (
"*.txt" )
// select fileInfo;

// foreach ( var fileInfo in fileQuery )
// Console.WriteLine ( fileInfo.Name );
// }
}
}

then in form1 i replace the commented code above with:
private void button1_Click(object sender, EventArgs e)
{
DoSearch();
}
private void DoSearch()

{
string path = "C:\\0\\0info\\dotnetinfo";
DirectoryInfo root = new DirectoryInfo(path);

var fileQuery = from fileInfo in
root.EnumerateFiles("*.txt")
select fileInfo;
listView1.View = View.Details;
listView1.Columns.Add("Name");

foreach (var fileInfo in fileQuery)
this.listView1.Items.Add(fileInfo.Name);

}

so i'm not seeing where the console window is being asked to open.
sorry for the newb questions.
and thanks again for the code sample this is great!


thanks
mark
 
K

kndg

Meanwhile I'm messing with an alternate display, commenting out the
console.writeline and substituting a form with listview, to display the
filenames.
What I'm trying to figure out is why the console window is still opening,
even though i've commented out that code and find no other lines calling it
to open.
[...]

It is because you are modifying a *console* application. That's why you
still see a console window.
One of the solutions (easiest) is to add a *windows* application
project, set it as start-up project and work your code from there.
Another way (if you insist to modify existing project) is to change the
project type to windows application (right-click your project and choose
property and then set the output type to windows application). You may
need to add a reference to System.Windows.Forms.dll (which I think you
already done that).

Regards.
 
M

mp

kndg said:
Meanwhile I'm messing with an alternate display, commenting out the
console.writeline and substituting a form with listview, to display the
filenames.
What I'm trying to figure out is why the console window is still opening,
even though i've commented out that code and find no other lines calling
it
to open.
[...]

It is because you are modifying a *console* application. That's why you
still see a console window.
One of the solutions (easiest) is to add a *windows* application project,
set it as start-up project and work your code from there.
Another way (if you insist to modify existing project) is to change the
project type to windows application (right-click your project and choose
property and then set the output type to windows application). You may
need to add a reference to System.Windows.Forms.dll (which I think you
already done that).

Regards.

voila!
I thought it would be something like that but when i looked at properties
before I didn't see that. Thanks that fixed it.
Mark
 
M

mp

Tom Shelton said:
mp pretended :

If nothing else, it will show you how to use the api's :)

Tom,
Thanks again for providing that demo code.
I've been studying it and a good deal is over my head, but i thought I could
use the dll in another project
so i copied FileSystemEnumerator.dll to the other project's folder
added a reference to it
added a using FireAnt.IO; line to the using section
but i get the following errors
(the project compiles with no errors, but when i try to debug run it, the
error pops up)
Error 1 The type or namespace name 'FileSystemEnumerator' could not be found
(are you missing a using directive or an assembly reference?)
Error 2 'System.IO.DirectoryInfo' does not contain a definition for
'EnumerateFiles' and no extension method 'EnumerateFiles' accepting a first
argument of type 'System.IO.DirectoryInfo' could be found (are you missing a
using directive or an assembly reference?)

it's weird because FileSystemEnumerator is there in the references
when i dbl click on FileSystemEnumerator in the references it comes up in
object browser with following tree
+FireAnt.IO
+ FileSystemExtensions
in your demo project (which is a solution with two projects in it...maybe
that's what i have to do is add the entire project for FileSystemEnumerator
not just a reference to the built dll???)
anyway, in your demoproject the object browser has more in it
+FireAnt.IO
+ FileSystemExtensions
+NativeWin32
+NativeWin32.Filetime
+NativeWin32.SafeSearchHandle
+NativeWin32.Win32_Find_Data

so am i wrong thinking that FileSystemEnumerator.dll has all the
functionality compiled into it?

thanks
mark
 
M

mp

mp said:
Tom,
Thanks again for providing that demo code. snip'

so am i wrong thinking that FileSystemEnumerator.dll has all the
functionality compiled into it?

thanks
mark

yep, that worked
that's what i had to do is add the entire project for FileSystemEnumerator
not just a reference to the built dll
i don't understand the why of that, having two 'projects' in one 'solution'
but at least it worked
thanks
mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top