2011-10-08 167 views
4

即时通讯新手到c#.net,任何一个让我知道如何获取文件大小,文件名,在C#Windows文件分机。如何在C#Windows中获取文件大小,文件名,文件扩展名?

IM使用C#的打开文件对话框..即时通讯只获得了path..i不知道如何获取文件名和大小..

我的代码是:

openFileDialog1.ShowDialog(); 

openFileDialog1.Title = "Select The File"; 
openFileDialog1.InitialDirectory = "C:"; 
openFileDialog1.Multiselect = false; 
openFileDialog1.CheckFileExists = false; 

if (openFileDialog1.FileName != "") 
{ 
    txtfilepath1.Text = openFileDialog1.FileName; 
    var fileInfo = new FileInfo(openFileDialog1.FileName); 
    lblfilesize1.Text = Convert.ToString(openFileDialog1.FileName.Length); 

    lblfilesize= 
    lblfilename=  
} 

回答

2

您可以使用FileInfo类。 File Info

using System; 
using System.IO; 

class Program 
{ 
    static void Main() 
    { 
    // The name of the file 
    const string fileName = "test.txt"; 

    // Create new FileInfo object and get the Length. 
    FileInfo f = new FileInfo(fileName); 
    long s1 = f.Length; 

    // Change something with the file. Just for demo. 
    File.AppendAllText(fileName, " More characters."); 

    // Create another FileInfo object and get the Length. 
    FileInfo f2 = new FileInfo(fileName); 
    long s2 = f2.Length; 

    // Print out the length of the file before and after. 
    Console.WriteLine("Before and after: " + s1.ToString() + 
     " " + s2.ToString()); 

    // Get the difference between the two sizes. 
    long change = s2 - s1; 
    Console.WriteLine("Size increase: " + change.ToString()); 
    } 
} 

对于扩展你可以使用Path.GetExtension()