Как получить размер файла, имя файла, файл Ext в C # Windows? - PullRequest
4 голосов
/ 08 октября 2011

я новичок в c # .net, любой, дайте мне знать, как получить размер файла, имя файла, файл Ext В C # Windows.

я использую диалог открытия файла в C # .. я получаю только путь..я не знаю, как получить имя и размер файла ..

мой код:

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=    
 }

Ответы [ 3 ]

14 голосов
/ 08 октября 2011

Вам не нужно использовать какой-либо другой класс.Вы уже используете FileInfo.

2 голосов
/ 08 октября 2011

Вы можете использовать класс FileInfo. Информация о файле

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 ()

0 голосов
/ 08 октября 2011
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...