Imports nsFile = System.IONamespace WebModules Public Class File '文件上传 '通过FileUp_FileReceived事件 '拷贝文件 Public Sub CopyFile(ByVal SourcePath As String, ByVal NewPath As String) Dim nFile As nsFile.File Try nFile.Copy(SourcePath, NewPath) Catch exc As Exception End Try End Sub '文件存在 Public Shared Function FileExist(ByVal FileName As String) As Boolean Dim nFile As nsFile.File Return nFile.Exists(FileName) End Function '创建文件 Public Sub CreateFile(ByVal FileName As String) Dim nFile As nsFile.File Dim nPath As String nPath = FileName.Substring(0, FileName.LastIndexOf("\")) If FileExist(FileName) Then DelFile(FileName) Else If Not FolderExist(nPath) Then CreateFolder(nPath) End If End If Try nFile.Create(FileName).Close() Catch exc As nsFile.IOException End Try End Sub '创建文件 Public Function OpenFile(ByVal FileName As String) As nsFile.TextWriter Dim nFile As nsFile.File Dim nPath As String nPath = FileName.Substring(0, FileName.LastIndexOf("\")) If Not FileExist(FileName) Then CreateFile(FileName) End If Try Return nFile.AppendText(FileName) Catch exc As nsFile.IOException Throw (exc) End Try End Function '创建文件 Public Function ReadFile(ByVal FileName As String) As String Dim nFile As nsFile.File Dim nReader As nsFile.StreamReader Dim nPath As String nPath = FileName.Substring(0, FileName.LastIndexOf("\")) If Not FileExist(FileName) Then Exit Function End If Try nReader = nFile.OpenText(FileName) ReadFile = nReader.ReadToEnd nReader.Close() Catch exc As nsFile.IOException Throw (exc) End Try End Function '列举文件 Public Function ListFile(ByVal FileName As String) As IEnumerator End Function '删除文件 Public Shared Sub DelFile(ByVal FilePath As String) Dim nFile As nsFile.File If FileExist(FilePath) Then Try nFile.Delete(FilePath) Catch exc As Exception End Try End If End Sub '清理文件夹 Public Shared Sub ClearFolder(ByVal FolderPath As String) Dim nFile As nsFile.File Dim nDir As nsFile.Directory Dim iArr As Int16 Dim nArr As String() = nDir.GetDirectories(FolderPath) For iArr = 0 To nArr.GetUpperBound(0) Try delfolder(nArr(iArr)) Catch exc As Exception End Try Next End Sub '文件夹存在 Public Shared Function FolderExist(ByVal FolderPath As String) As Boolean Dim nDir As nsFile.Directory Return nDir.Exists(FolderPath) End Function '创建文件夹 Public Shared Sub CreateFolder(ByVal FolderPath As String) Dim nDir As nsFile.Directory If Not FolderExist(FolderPath) Then Try nDir.CreateDirectory(FolderPath) Catch exc As Exception End Try End If End Sub '删除文件夹 Public Shared Sub delfolder(ByVal FolderPath As String) Dim nDir As nsFile.Directory If FolderExist(FolderPath) Then Try nDir.Delete(FolderPath, True) Catch exc As Exception End Try End If End Sub '转换路径,由URL路径转换成文件路径 Public Shared Function ChangePath(ByVal UrlPath As String, ByVal UrlRootPath As String, ByVal AppPath As String) As String Dim rtnPath As String rtnPath = System.IO.Path.Combine(AppPath, UrlPath.Replace(UrlRootPath, "").Replace("/", "\")) Return rtnPath End Function '转换路径,由文件路径转换成URL路径 Public Shared Function ChangeToURL(ByVal FilePath As String, ByVal UrlRoot As String, ByVal AppPath As String) As String Dim RtnUrl As String RtnUrl = UrlRoot & FilePath.Replace(AppPath, "").Replace("\", "/") Return RtnUrl End Function '转换路径,由文件路径转换成URL路径 Public Shared Function ChangeToURL(ByVal FilePath As String, ByVal AppPath As String) As String Dim RtnUrl As String RtnUrl = "/" & FilePath.Replace("\", "/").Replace(AppPath.Replace("\", "/"), "") Return RtnUrl End Function '获取近似无重复文件夹名称 Public Overloads Shared Function IdentityFoldName() As String Return DateTime.Now.Ticks.ToString End Function '获取绝对无重复文件夹名称 Public Overloads Shared Function IdentityFoldName(ByVal ParentFoler As String) As String Dim IdtFoleName As String = IdentityFoldName() While FolderExist(ParentFoler & "\" & IdtFoleName) IdtFoleName = IdentityFoldName() End While Return IdtFoleName End Function End Class End Namespace
Dim objFileStream As FileStream Dim objStreamWriter As StreamWriter objFileStream = New FileStream(FILE_NAME, FileMode.Create, FileAccess.Write) objStreamWriter = New StreamWriter(objFileStream) head = strCss objStreamWriter.WriteLine(head) For i = 0 To sr.Length - 1 objStreamWriter.WriteLine(sr(i)) Next objStreamWriter.Close() objFileStream.Close() Response.Clear() Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(FILE_NAME)) 'Response.AddHeader("Content-Length", file1.Length.ToString()) Response.ContentType = "application/ms-excel" ‘===改为css Response.WriteFile(FILE_NAME) Response.End()
Public Class File
'文件上传
'通过FileUp_FileReceived事件 '拷贝文件
Public Sub CopyFile(ByVal SourcePath As String, ByVal NewPath As String)
Dim nFile As nsFile.File
Try
nFile.Copy(SourcePath, NewPath)
Catch exc As Exception
End Try
End Sub
'文件存在
Public Shared Function FileExist(ByVal FileName As String) As Boolean
Dim nFile As nsFile.File
Return nFile.Exists(FileName)
End Function
'创建文件
Public Sub CreateFile(ByVal FileName As String)
Dim nFile As nsFile.File
Dim nPath As String
nPath = FileName.Substring(0, FileName.LastIndexOf("\"))
If FileExist(FileName) Then
DelFile(FileName)
Else
If Not FolderExist(nPath) Then
CreateFolder(nPath)
End If
End If
Try
nFile.Create(FileName).Close()
Catch exc As nsFile.IOException End Try
End Sub
'创建文件
Public Function OpenFile(ByVal FileName As String) As nsFile.TextWriter
Dim nFile As nsFile.File
Dim nPath As String
nPath = FileName.Substring(0, FileName.LastIndexOf("\"))
If Not FileExist(FileName) Then
CreateFile(FileName)
End If
Try
Return nFile.AppendText(FileName)
Catch exc As nsFile.IOException
Throw (exc)
End Try
End Function
'创建文件
Public Function ReadFile(ByVal FileName As String) As String
Dim nFile As nsFile.File
Dim nReader As nsFile.StreamReader
Dim nPath As String
nPath = FileName.Substring(0, FileName.LastIndexOf("\"))
If Not FileExist(FileName) Then
Exit Function
End If
Try
nReader = nFile.OpenText(FileName)
ReadFile = nReader.ReadToEnd
nReader.Close()
Catch exc As nsFile.IOException
Throw (exc)
End Try
End Function
'列举文件
Public Function ListFile(ByVal FileName As String) As IEnumerator End Function '删除文件
Public Shared Sub DelFile(ByVal FilePath As String)
Dim nFile As nsFile.File
If FileExist(FilePath) Then
Try
nFile.Delete(FilePath)
Catch exc As Exception End Try
End If
End Sub '清理文件夹
Public Shared Sub ClearFolder(ByVal FolderPath As String)
Dim nFile As nsFile.File
Dim nDir As nsFile.Directory
Dim iArr As Int16
Dim nArr As String() = nDir.GetDirectories(FolderPath)
For iArr = 0 To nArr.GetUpperBound(0)
Try
delfolder(nArr(iArr))
Catch exc As Exception End Try
Next
End Sub '文件夹存在
Public Shared Function FolderExist(ByVal FolderPath As String) As Boolean
Dim nDir As nsFile.Directory
Return nDir.Exists(FolderPath)
End Function
'创建文件夹
Public Shared Sub CreateFolder(ByVal FolderPath As String)
Dim nDir As nsFile.Directory
If Not FolderExist(FolderPath) Then
Try
nDir.CreateDirectory(FolderPath)
Catch exc As Exception End Try
End If
End Sub '删除文件夹
Public Shared Sub delfolder(ByVal FolderPath As String)
Dim nDir As nsFile.Directory
If FolderExist(FolderPath) Then
Try
nDir.Delete(FolderPath, True)
Catch exc As Exception End Try
End If
End Sub '转换路径,由URL路径转换成文件路径
Public Shared Function ChangePath(ByVal UrlPath As String, ByVal UrlRootPath As String, ByVal AppPath As String) As String
Dim rtnPath As String
rtnPath = System.IO.Path.Combine(AppPath, UrlPath.Replace(UrlRootPath, "").Replace("/", "\"))
Return rtnPath
End Function
'转换路径,由文件路径转换成URL路径
Public Shared Function ChangeToURL(ByVal FilePath As String, ByVal UrlRoot As String, ByVal AppPath As String) As String
Dim RtnUrl As String
RtnUrl = UrlRoot & FilePath.Replace(AppPath, "").Replace("\", "/")
Return RtnUrl
End Function '转换路径,由文件路径转换成URL路径
Public Shared Function ChangeToURL(ByVal FilePath As String, ByVal AppPath As String) As String
Dim RtnUrl As String
RtnUrl = "/" & FilePath.Replace("\", "/").Replace(AppPath.Replace("\", "/"), "")
Return RtnUrl
End Function '获取近似无重复文件夹名称
Public Overloads Shared Function IdentityFoldName() As String
Return DateTime.Now.Ticks.ToString
End Function '获取绝对无重复文件夹名称
Public Overloads Shared Function IdentityFoldName(ByVal ParentFoler As String) As String
Dim IdtFoleName As String = IdentityFoldName()
While FolderExist(ParentFoler & "\" & IdtFoleName)
IdtFoleName = IdentityFoldName()
End While
Return IdtFoleName
End Function
End Class
End Namespace
Dim objStreamWriter As StreamWriter
objFileStream = New FileStream(FILE_NAME, FileMode.Create, FileAccess.Write)
objStreamWriter = New StreamWriter(objFileStream) head = strCss
objStreamWriter.WriteLine(head)
For i = 0 To sr.Length - 1
objStreamWriter.WriteLine(sr(i))
Next
objStreamWriter.Close()
objFileStream.Close()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(FILE_NAME))
'Response.AddHeader("Content-Length", file1.Length.ToString())
Response.ContentType = "application/ms-excel" ‘===改为css
Response.WriteFile(FILE_NAME)
Response.End()
xmlpath是文件的路径
*/public bool UpdateTreeXML(string xmlpath)
{
StreamWriter sw;
string FilePath = xmlpath == null ? "" : xmlpath;
try
{
if(File.Exists(FilePath))
File.Delete(FilePath);
sw = File.CreateText(FilePath);
sw.Write(GetTreeXML(this.Customer_ID));
sw.Close();
sw = null;
}
catch
{
sw = null;
return false;
}
return true;
}