Visual C # Summary of programming skills

Source: Internet
Author: User

Get the file version information:

FileVersionInfo myFileVersionInfo1 = FileVersionInfo. GetVersionInfo ("D: \ TEST. DLL ");
TextBox1.Text = "version:" + myFileVersionInfo1.FileVersion;

Modify file attributes and delete read-only files:

In the following example, you want to copy the E: est.txt file to D: mpest.txt, but D: mpest.txt already exists.

// File. Copy (sourceFile, destinationFile, true); used to Copy an object
// When destinationFile already exists, you cannot copy file file1 to the target file,
// Delete the destination File first. The File. Delete () method cannot Delete the read-only File,
// Therefore, if the file attribute is read-only (the Attributes attribute contains "ReadOnly "),
// First reset the file property to Normal and then delete the file:
String file1 = "E: \ test.txt ";
String destinationFile = "d: \ tmp \ test.txt ";
If (File. Exists (destinationFile ))
{
FileInfo fi = new FileInfo (destinationFile );
If (fi. Attributes. ToString (). IndexOf ("ReadOnly ")! =-1)
Fi. Attributes = FileAttributes. Normal;
File. Delete (destinationFile );
}
File. Copy (file1, destinationFile, true );

How to format and convert a string to a value in C #

Convert a string to a number, for example, "1234" to a number 1234:

String str = "1234 ";
Int I = Convert. ToInt32 (str );

Format the String, add a specific character to the end of a String of less than 30 to supplement n characters, and use the PadRight (int, char) method of the String class:

String str = "1234 ";
Str = str. PadRight (30,) // Add a space to the end of a string less than 30 characters to Supplement 30 characters

Read and Write files by row

Determine whether the File Exists: File. Exists (string filePath)

Determine whether the Directory Exists: Directory. Exists ("D: \ LastestVersion ")

Read files by row:

Int fileCount = 0;
// Open the file just specified such that no one else can use it.
StreamReader sr = new StreamReader (textBox1.Text. Trim ());
While (sr. Peek ()>-1) // StreamReader. Peek () returns the next available character, but does not use it
{
ListBox1.Items. Add (sr. ReadLine ());
FileCount ++;
}
Sr. Close ();

Write files by row:

StreamWriter sw = new StreamWriter ("D: \ result.txt ");
For (int I = 0; I <10; I ++)
{
Sw. WriteLine ("this is the" + I. ToString () + "row data ");
}

Use of the file directory dialog box

The file dialog box shows how to use the filter conditions:

String resultFile = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
OpenFileDialog1.InitialDirectory = "D: \ Patch ";
OpenFileDialog1.Filter = "All files (*. *) | *. * | txt files (*. txt) | *. txt ";
OpenFileDialog1.FilterIndex = 2;
OpenFileDialog1.RestoreDirectory = true;
If (openFileDialog1.ShowDialog () = DialogResult. OK)
ResultFile = openFileDialog1.FileName;

Use of the Directory dialog box:

String resultFolder = "";
FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog ();
OpenFolderDialog1.RootFolder = Environment. SpecialFolder. MyComputer;
If (openFolderDialog1.ShowDialog () = DialogResult. OK)
ResultFolder = openFolderDialog1.SelectedPath;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.