Current location - Quotes Website - Personality signature - Project type issues in c++
Project type issues in c++

On the File menu, click New Project.

The "New Project" dialog box appears. This dialog box lists the different default application types that Visual C# Express can create.

Select "Console Application" as the project type and change the name of the application to "List Files".

The default location should be appropriate, but you can always enter a new path if needed.

Click OK.

Visual C# Express creates a new folder for your project named with the project title, then opens the main Visual C# Express window, including the Code pane, where you will enter and edit the components that make up your application C# source code.

Note the toolbar at the top of the window. The toolbar contains icons for creating, loading, and saving projects, editing source code, building applications, and hiding and showing other windows that make up the Visual C# Express environment. The five icons on the far right of the toolbar are used to open important windows, such as Solution Explorer and Toolbox. Place your mouse pointer over any of these icons to get pop-up tooltip help.

Note

Bookmarks allow you to quickly jump from one location in the source code to another, so it is useful for writing large programs. To create a bookmark, click the Toggle Bookmark icon or press Ctrl+B+T. A cyan marker appears in the margin. Use the same process to delete existing bookmarks. You can create as many bookmarks as you like and jump between them using the Next and Previous bookmark icons or by pressing Ctrl+B+N and Ctrl+B+P.

Make sure Solution Explorer is visible by clicking the Solution Explorer tab at the right end of the screen or the Solution Explorer icon in the toolbar.

Solution Explorer is a very useful pane because it displays the various files that make up the project. The most important file in the project is the "Program.cs" file, which contains the source code of the application.

If you want to keep the display of Visual C# Express nice and clean, it's important to know how to open and hide windows like Solution Explorer. By default, Solution Explorer is visible. If you want to hide Solution Explorer, click the Autohide icon (the pushpin icon) in its title bar, or open the Options menu in the Solution Explorer title bar and enable AutoHide hide". Other windows such as Class View and Properties also have these icons.

Type the class name "Console" in the "Code Editor".

If Solution Explorer is still blocking the Code pane, click once in the Code pane to hide Solution Explorer. Now click to the right of the opening brace ({) inside the Main method and press Enter to start a new line. Notice how the editor automatically indents the cursor.

Note

The Code Editor always attempts to format your code into a standard, easy-to-read layout. If your code starts to look cluttered, you can reformat the entire document by clicking Advanced, then clicking Format Document on the Edit menu, or pressing Ctrl+E+D.

When you type a C# class name or keyword, you have the choice: type the complete word yourself, or let the IntelliSense tool (part of the Code pane) do it for you. For example, when you type "c", a pop-up list of words appears as IntelliSense tries to predict the word you want to type. In this case, you won't see the word "Console" that was just shown, so either scroll down the list or continue typing the word "console." When "console" is highlighted in the list, press Enter or Tab, or double-click it, and the Console will be added to the code.

The benefit of using IntelliSense is that you can ensure correct capitalization and spelling. It's up to you whether you type your code or let IntelliSense do it for you.

Type a period and the method name WriteLine.

When you type a period after Console, another IntelliSense list will appear immediately. This list contains all possible methods and properties belonging to the Console class. You need the WriteLine method and should be able to see it at the bottom of the list. To complete it on its own, type WriteLine or press the Down arrow key to select it, then press Enter or Tab or double-click it. WriteLine will be added to the code.

Type the opening bracket. You'll immediately see another feature of IntelliSense—method signatures, displayed as tooltip messages.

In this example, you can see 19 different signatures and browse through them by clicking the up and down arrow keys.

Type the string "This program lists all files in a directory."

Type the message within quotation marks and add a closing parenthesis. You will see a red wavy underline appear to remind you that some symbols are missing. Type a "semicolon" (;) and the underline will disappear.

Complete the procedure.

Type or copy and paste the following code to complete the program:

C#

Copy code

static void Main(string[] args ) { Console.WriteLine("This program lists all the files in the directory:"); System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\"); foreach (System.IO.FileInfo file in dir.GetFiles("*.*")) { Console.WriteLine("{0}, {1}", file.Name, file.Length); } Console.ReadLine(); }

The last line in the program is Console.ReadLine();, which causes the program to pause before pressing Enter. If you omit this line, the command line window will disappear immediately and you will not see the program's output. If you are creating a command-line utility that is always used from a command-line console, you may want to omit the call to the ReadLine() method.

Run the program.

Your first program is now complete and ready to compile and run. To do this, press F5 or click the Start icon in the toolbar.

After the program compiles and runs, the Console window opens and displays a list of files and their sizes. Press Enter to exit the program.

If you are new to C# programming, it is a good idea to read the Getting Started with the C# Language (Visual C# Express Edition) section and experiment with some of the language examples. If you would like to learn more about the Visual C# Express development environment and how to create a Windows application, continue to the next section How to: Create a C# Windows Application.

As you can see from the specific statement below, after determining the name of the file to be found and the name of the directory to be searched, the function Search_Directory will be called to search for the file. First, search each entity (file or subdirectory) in the current directory in sequence. If it is a subdirectory, enter the subdirectory and recursively call the function Search_Directory to search. After the search is completed, return to the upper directory; if If it is not a subdirectory but a certain file, then determine whether it is the file we are looking for, and if so, output its complete file path. In this way, through repeated recursive calls of the Search_Directory function, the entire directory, including subdirectories, can be traversed and searched. The following will give an example of how to program in VC++ to search for files in the entire directory tree.

1. In Visual C++ 6.0 (VC++ 5.0 is similar), a dialog-based application Search is created by default. Place a command button on the main window dialog box with the Caption "Search File" and the ID ID-BUTTON-SEARCH. Clicking this button will complete the file search.

2. Use ClassWizard to add the handler function OnButtonSearch to the BN_CLICKED event of the "Search File" button. The code is as follows:

#include 〈direct.h〉

#include 〈io.h〉

......

void CSearchDlg::OnButtonSearch()

{

// TODO: Add your control notification handler code here

char szFilename[80];

// The string szFilename represents the file name to be found

strcpy(szFilename,〃Mytext.txt〃); < /p>

_chdir(〃d:\\〃); // Enter the path to be searched (it can also be a specific directory)

// Search for the file, and display it if found The full path name of the file

Search_Directory(szFilename);

// It is a member function of the CSearchDlg class

MessageBox(〃Searching for files completed!〃);

// Display the searched information

}

3. Add the member function Search_Directory to the CSearchDlg class, which will complete the specific file search work. The code is as follows:

void CSearchDlg::Search_Directory(char* szFilename)

{

long handle;

struct _finddata_t filestruct;

//Represents file (or directory) information

char path_search[_MAX_PATH];

//Represents the found path result

//Start the search and find the first entity (file or subdirectory) in the current directory,

// 〃*〃 means to search for any file or subdirectory, filestruct is the search result

handle = _findfirst(〃*〃, &filestruct);

// If handle is -1, it means If the current directory is empty, end the search and return

if((handle == -1)) return;

// Check whether the first entity found is a directory ( filestruct.name is its name)

if( ::GetFileAttributes(filestruct.name) & FILE―ATTRIBUTE―DIRECTORY )

{

// If it is directory, then enter the directory and recursively call the function Search_Directory to search,

// Note: If the first character of the directory name is ′.′ (that is, 〃.〃 or 〃..〃), it is not necessary Search

if( filestruct.name[0] != ′.′ )

{

―chdir(filestruct.name);

Search_Directory(szFilename);

// After the search is completed, return to the previous directory

―chdir(〃..〃);

}

}

else // If the first entity is not a directory, check whether it is the file to be found

{

/ / stricmp compares the two strings in lower case, and returns 0 to indicate complete consistency

if( !stricmp(filestruct.name, szFilename) )

{

< p> // First get the full path of the current working directory

―getcwd(path_search,―MAX―PATH);

// Then get the complete path name of the file (including the file name)

strcat(path_search,〃\\〃);

strcat(path―search,filestruct.name);

MessageBox(path_search); //Output display

}

}

// Continue the same search as above for the next subdirectory or file in the current directory

while(!(―findnext(handle,&filestruct)))

{

if( ::GetFileAttributes(filestruct.name) & FILE―ATTRIBUTE―DIRECTORY ) < /p>

{

if(*filestruct.name != ′.′)

{

―chdir(filestruct.name); < /p>

Search_Directory(szFilename);

―chdir(〃..〃);

}

}

else

{

if(!stricmp(filestruct.name,szFilename))

{

―getcwd(path―search, ―MAX―PATH);

strcat(path_search,〃\\〃);

strcat(path_search,filestruct.name);

MessageBox(path_search) ;

}

}

}

―findclose(handle);

//Finally end the entire Find a job