1. openFileDialog 控件
此控件调用系统打开文件的那个对话框。
首先从tools中把openFileDialog控件拖到form中。
然后
if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo EzoneFile = new FileInfo(this.openFileDialog1.FileName); this.textBox1.Text = EzoneFile.FullName; this.textBox2.Text = EzoneFile.Name; this.textBox3.Text = EzoneFile.Length.ToString(); }
2. 同时这里面还用到了文件对象。 FileInfo对象。声明一个此种对象,调用它的构造函数,参数是文件完全路径。
3. 路径在C#里面有两种表示方法
一种是
@"E:\old F directory\TangWei\kangge\new1.jpg";
"E:\\old F directory\\TangWei\\kangge\\new1.jpg";
4. 文件流对象FileStream对象
使用指定的路径、创建模式和读/写权限初始化 FileStream 类的新实例。不需要指定流对象的大小,只需要特定的文件地址。
public FileStream ( string path, FileMode mode, FileAccess access ) //Example FileStream MyFileStream = new FileStream(SendFileName_full_addr, FileMode.Create, FileAccess.Write);
5. 文件夹对象DirectoryInfo对象 使用指定的路径来初始化一个文件夹对象。 使用foreach来遍历其实的每一个文件对象。string folderPath = @"E:\old F directory\TangWei\kangge"; DirectoryInfo di = new DirectoryInfo(folderPath); FileInfo[] fis = di.GetFiles("*.*"); foreach(FileInfo fi in fis) { UpLoadOneFile(fi); // Thread.Sleep(1000); // 这个1000值得商榷 }
6. StreamWriter 把字符写到文本里面去
StreamWriter sw = new StreamWriter("C://Documents and Settings//Tang//Desktop//jpeg//tem_pic//time.txt", true); sw.WriteLine(i); sw.WriteLine(sum); avr = sum / i; sw.WriteLine(avr); sw.Close();
C# File.Open Examples
Dot Net PerlsHow can you use the File.Open method in your C# program?Found in the System.IO namsespace, File.Open returns a FileStreamthat you can use. The arguments you pass to it determine its action.Here, we provide examples of File.Open.
Example
Let's examine this program. First, we use File.Open as part of the using-resource-acquisitionpattern; this ensures proper recovery of system resources.The first argument to File.Open is the name of the file we are acting upon.The second argument is a FileMode enumerated constant.With FileMode.Create, we create a new file (or create it again if it already exists).With FileMode.Open, we open an existing file.
Using Statement Calls DisposeProgram that uses File.Open [C#] using System; using System.IO; class Program { static void Main() { using (FileStream stream = File.Open("C:\\bin", FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(303); writer.Write(720); } using (FileStream stream = File.Open("C:\\bin", FileMode.Open)) using (BinaryReader reader = new BinaryReader(stream)) { int a = reader.ReadInt32(); int b = reader.ReadInt32(); Console.WriteLine(a); Console.WriteLine(b); } } } Output 303 720BinaryWriter and BinaryReader.The BinaryWriter and BinaryReader types are a practical use of FileStream instances.These constructors receive Stream references.Because File.Open returns a FileStream, and FileStream is derived from Stream,we can use FileStream as a Stream in the constructors. The cast is implicit.
Stream TypeBinaryWriter TutorialBinaryReader TutorialRound-tripping.We can see in this program that the numbers 303 and 720 are written to the "C:\bin"file. Then, they are read back into integers from that same file. (As an aside,this is probablyone of the most inefficient ways to write two numbers to the screen.)
FileMode
Let's briefly review the FileMode enumerated type.To use FileMode, type FileMode and press the period key, and then read thedescriptions for each constant in Visual Studio.The constants are fairly self-descriptive.
FileMode.Append
FileMode.Create
FileMode.CreateNew
FileMode.Open
FileMode.OpenOrCreate
FileMode.TruncateFile.Open alternatives
Although File.Open serves as a general-purpose solution when paired with FileMode,you can instead call File.OpenRead, File.OpenText, and File.OpenWrite.Conceptually, these methods are the same as using File.Open with certain FileModearguments.
Summary
We looked at practical examples of the File.Open method, and also described the FileModeenumerated type.With the using statement, and the implicit casting in the C# language,we can use File.Open as part of a powerful file-handling construction.