A summary of Visual C # programming techniques

Source: Internet
Author: User
visual| Program | tips | Design Get version information for a file:

FileVersionInfo myFileVersionInfo1 = Fileversioninfo.getversioninfo ("D:\\test.") DLL ");
Textbox1.text= "version number:" + myfileversioninfo1.fileversion;

To change file properties and delete read-only files:

The following example wants to copy the E:\test.txt file to D:\tmp\test.txt, but D:\tmp\test.txt already exists.

File.Copy (sourcefile,destinationfile,true); Used to copy files
The file file1 cannot be copied to the destination file when Destinationfile already exists.
Therefore, delete the destination file first and the File.delete () method cannot delete the read-only file.
Therefore, if the file property is read-only ("ReadOnly" is included in the Attributes property),
Reset the file properties to normal before deleting them:
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);

   C #The format of the string and the method of converting it into a numerical value

Converts a string to a number, such as "1234" to the number 1234:

String str= "1234";
int I=convert.toint32 (str);

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

String str= "1234";
Str=str. PadRight (30, ')///To add a space to the end of a string that is less than 30, to complement 30 characters

Read and write files by line

To determine if a file exists: File.exists (string FilePath)

Determine if the directory exists: directory. Exists ("D:\\lastestversion")

To read a file 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 ();

To write a file by line:

StreamWriter sw = new StreamWriter ("D:\\result.txt");
for (int i=0;i<10;i++)
{
Sw. WriteLine ("This is the first" +i.tostring () + "row data");
}

Use of the File Directory dialog box

The file dialog box is the use of 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;


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.