To obtain 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);
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");
}
Method Two:
Private Const int fo_delete = 0x3;
Private Const ushort Fof_noconfirmation = 0x10;
Private Const ushort Fof_allowundo = 0x40;
[DllImport ("Shell32.dll", Setlasterror=true, CharSet=CharSet.Unicode)]
private static extern int SHFileOperation ([in,out] _shfileopstruct str);
[StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public class _shfileopstruct
{
Public INTPTR hwnd;
Public UInt32 Wfunc;
public string pfrom;
public string PTo;
Public UInt16 fflags;
Public Int32 fanyoperationsaborted;
Public IntPtr hnamemappings;
public string Lpszprogresstitle;
}
public static int Delete (string path)
{
_shfileopstruct pm = new _shfileopstruct ();
Pm.wfunc = Fo_delete;
Pm.pfrom = path + ' ";
Pm.pto = null;
Pm.fflags = fof_allowundo¦fof_noconfirmation;
Return SHFileOperation (PM);
}
Call: Class1.delete ("C:\\temp.txt");
Note: A return value of 0 indicates a successful call, and a non-0 indicates that the call failed.
You need to reference the System.Runtime.InteropServices namespace.