1. ASP.NET 如何獲得文件夾大小
例如可以用以下方法: 其中參數path所傳遞的值是所要查看的文件夾的絕對路徑! 首先要導入System.IOC#//獲取某個文件夾的大小 (stringpath){DirectoryInfodirInfo=newDirectoryInfo(path); longsumSize=0; FileSystemInfofsInfo; foreach(fsInfoindirInfo.GetFileSystemInfos()){if(fsInfo.Attributes.ToString().ToLower()==directory){sumSize+=getDirectorySize(fsInfo.FullName);}else{FileInfofiInfo=newFileInfo(fsInfo.FullName); sumSize+=fiInfo.Length;}}returnsumSize;}VB.NET '獲取某個文件夾的大小 FunctiongetDirectorySize(ByValpathAsString)AsLong DimdirInfoAsNewDirectoryInfo(path) DimsumSizeAsLong=0 DimfsInfoAsFileSystemInfo ForEachfsInfoIndirInfo.GetFileSystemInfos() If(fsInfo.Attributes.ToString.ToLower=directory)Then sumSize+=getDirectorySize(fsInfo.FullName)Else DimfiInfoAsNewFileInfo(fsInfo.FullName) sumSize+=fiInfo.LengthEndIfNext
2. C# ASP.NET 使用 FileUpload如何判斷要上傳文件大小
try
public static long GetRemoteHTTPFileSize(string sURL)
{
long size = 0L;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
request.Method = "HEAD";
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
size = response.ContentLength;
//size = System.Convert.ToInt64(response.Headers["Content-Length"]);
response.Close();
}
catch
{
size = 0L;
}
return size;
}
3. vb.net怎麼獲取mp3文件的大小
Dimmp3FileAsNewSystem.IO.FileInfo("C:示例代碼.mp3")
MsgBox("文件大小為:"&mp3File.Length&"位元組(Byte)")
如果需要其他單位,比如KB、MB之類的,除以1024就可以了。
4. vbnet 如何獲取文件大小
Dim fi As New System.IO.FileInfo("C:\1.txt")
MsgBox(fi.Length)
5. ASP.NET里的文件大小判別
這是在伺服器端判斷的,但.NET 默認限制的大小是4M,超過了就會出現問題
可以在web.config里的system.web內增加這個限制,比如加到200M,這樣200M內的文件就能判斷了
<system.web>
<httpRuntime executionTimeout="600" maxRequestLength="2048000"
useFullyQualifiedRedirectUrl="true" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true"/>
</system.web>
6. .net中如何通過用js來獲取上傳文件的大小,判斷大小...
下面是方法和問題。
JS客戶端判斷filesize
<script language="JavaScript">
function ShowSize(files)
{
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(files);
if(f.Size >1024*5){
alert("文件尺寸"+Math.round(f.size/1024,2)+" K,照片尺寸不能超過5K!");
}
}
</script> 這種方式必專須打開:「屬對沒有標記為安全的ActiveX控制項進行初始化和腳本運行 ->啟用」 否則就提示錯誤 所以建議你,不光js要判斷,後台還是要配置和判斷。
7. 用wininet的InternetReadFile如何得知文件大小
Visual C++
如何:讀取二進制文件
下面的代碼示例演示如何從文件中讀取二進制數據。使用了 System.IO 命名空間中的兩個類:FileStream 和 BinaryReader。FileStream 表示實際的文件。BinaryReader 為允許二進制訪問的流提供介面。
下面的代碼示例使用由如何:編寫二進制文件中的代碼創建的稱為 data.bin 的文件。
示例
// binary_read.cpp
// compile with: /clr
#using<system.dll>
using namespace System;
using namespace System::IO;
int main()
{
String^ fileName = "data.bin";
try
{
FileStream^ fs = gcnew FileStream(fileName, FileMode::Open);
BinaryReader^ br = gcnew BinaryReader(fs);
Console::WriteLine("contents of :", fileName);
while (br->BaseStream->Position < br->BaseStream->Length)
Console::WriteLine(br->ReadInt32().ToString());
fs->Close( );
}
catch (Exception^ e)
{
if (dynamic_cast<FileNotFoundException^>(e))
Console::WriteLine("File '' not found", fileName);
else
Console::WriteLine("Exception: ()", e);
return -1;
}
return 0;
}
★★補充★★
手上的一個VB項目(過程中發現,.Net果然是好啊),需要在一個ActiveX中實現HTTP下載功能,我是採用InternetreadFile這個API來實現,一開始的代碼我是這么寫的
Function Gethttpdownload(sUrl As String) As boolen
Dim s As String
Dim hOpen As Long
Dim hOpenUrl As Long
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim sReadBuffer As String * 2048
Dim lNumberOfBytesRead As Long
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bDoLoop = True
Do While bDoLoop
sReadBuffer = vbNullString
bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
s = s & Left$(sReadBuffer, lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
Loop
FileName = "E:\BitSpirit\Torrent\121212.torrent"
F1 = FreeFile
Open FileName For Binary As F1
Put F1, , s
Close F1
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Gethttpdownload = true
End Function
上面方法,用來獲取伺服器上的文本類型的文件一點問題,都沒有,但是用來下載二進制文件的時候 就出現問題了,裡面的數據怎麼也不對,研究了下載下來的文件後發現,問題外話應該是在接收數據的變數是個STRING的定長字元串上。但是在網上查了好久,甚至M$ MSDN上的一個用VB來實現下載的程也是用 string類型來接收數據的而且網上的代碼寫法,基本上也都是這個樣,好來才好現,都是從MSDN上的哪個常式上演變過來的.
我想如果能用一個byte數組來代替定長字元串,哪可能就沒有問題了,但是查看了一個VB 對Internetreadfile的申明
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
發現其定義ByVal sBuffer As String 看來只能用String了,在網上查找過程中,發現人家用VC寫的程序中這人參數可以是其它的,所以查看了一下Internetreadfile的原型。發現的確可以, 所以我把internetreadfile的定義修改了一下,為了通用,我為新的internetreadfile定義了一個別名。Internetreadfilebyte申明如下:
Public Declare Function InternetReadFileByte Lib "wininet.dll" Alias "InternetReadFile" (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
試了一下的確可以,重新修改函數,(在這過程中發現,如果要取到正確數據,還必須取得文件大小。所以增加了HttpQueryInfo的定義)最後完整的函數
Function FileDownload(sUrl As Variant) As Boolean
Dim b(99) As Byte
Dim EndByte() As Byte
Dim s As String
Dim hOpen As Long
Dim hOpenUrl As Long
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim bbuffer As Byte
Dim sReadBuffer As String
Dim FileName As String
Dim lNumberOfBytesRead As Long
Dim F1 As Integer
Dim strsize As String
Dim size As Long
strsize = String$(1024, " ")
F1 = FreeFile
stTotal = vbNullString
FileName = "E:\BitSpirit\Torrent\121212.torrent"
Open FileName For Binary As F1
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bDoLoop = True
HttpQueryInfo hOpenUrl, HTTP_QUERY_CONTENT_LENGTH Or HTTP_QUERY_FLAG_NUMBER, ByVal strsize, Len(strsize), 0
size = CLng(Trim(strsize))
For j = 1 To size \ 100
bDoLoop = InternetReadFileByte(hOpenUrl, b(0), 100, lNumberOfBytesRead)
Put F1, , b
If Not CBool(lNumberOfBytesRead) Then Exit For
Next
If size Mod 100 <> 0 Then
tmp = (size Mod 100) - 1
ReDim EndByte(tmp)
bDoLoop = InternetReadFileByte(hOpenUrl, EndByte(0), tmp + 1, lNumberOfBytesRead)
Put F1, , EndByte
End If
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Close #1
FileDownload = True
End Function
測試了一下,完全成功:)