重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
OpenFileDialog openFile = new OpenFileDialog();
创新互联公司是一家专业的成都网站建设公司,我们专注网站建设、网站设计、网络营销、企业网站建设,卖链接,一元广告为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。
openFile.Multiselect = true;
openFile.Filter = "图片 (*.jpg)|*.jpg|所有文件 (*.*)|*.*";
if (openFile.ShowDialog() == DialogResult.OK)
{
string ss = openFile.FileName;
string ww = openFile.FileName.Remove(openFile.FileName.Length - 4, 4);
File.Copy(ss, @"..\..\picture\" + Path.GetFileName(ss));
}
上面是先获取路径,在把图片复制到指定路径下。
您问的是Visual Studio .NET概念问题. 通常一个web项目里面,为了高效地管理开发工作所需要的项,如引用、数据连接、文件夹和文件,我们经常把数据访问类、自定义控件之类的分离出来作为一个单独包.Visual Studio .NET 提供了两个容器:解决方案和项目。查看和管理这些容器及其关联项的界面是解决方案资源管理器,它作为集成开发环境 (IDE) 的一部分提供。解决方案和项目包含一些项,这些项表示创建应用程序所需的引用、数据连接、文件夹和文件。解决方案容器可包含多个项目,而项目容器通常包含多个项。一个解决方案里面就包含了web项目,数据访问类的项目,控件包的项目了。 容器:解决方案和项目 解决方案和项目包含一些项,这些项表示创建应用程序所需的引用、数据连接、文件夹和文件。解决方案容器可包含多个项目,而项目容器通常包含多个项。使用这些容器以下列方式利用集成开发环境: 作为一个整体管理解决方案的设置或管理单个项目的设置使用解决方案资源管理器处理文件管理细节,在您集中精力于那些组成开发工作的项时添加对解决方案中多个项目有用或对该解决方案有用的项,而不必在每个项目中引用该项处理与解决方案或项目无关的杂项文件在生成解决方案的时候同时编译这三个项目,可以设置web项目自动引用其他的项目。 vb.net解决方案就是.NET环境下利用VB语言编制的解决方案. 新建一个项目,你需要这样做: 1. 首先在VS2005或2008中新建一个“网站”,选择“ASP.NET网站”,以“文件系统”的方式保存一个位置,比如:“E:\WebProject”。此时就会在E盘新生成一个网站项目WebProject,这个项目中只有一个默认的网页“Default.aspx”,你还可以在此项目中添家新的网页。 2. 在控制面板中打开IIS,创建虚拟目录,比如虚拟目录名为“WebTest”,并将虚拟目录指向刚刚建好的项目位置,即“E:\WebProject”。 3. 打开IE,在URL中输入“”,你便会看到网页"Default.aspx”的内容了。 这个问题需要学习Visual Studio .NET,建议到电脑数码-网页制作中询问可能会得到更好答案
已经做得差不多了呀,不过为了效率和广泛性,建议使用API来操作文件,而不是FSO对象
建议增加打开文件、移动文件、重命名文件、查看文件属性的功能
操作文件需要的API及声明:
复制文件:
1.CopyFile 2.CopyFileEx 3.ReadFile-WriteFile 4.NtReadFile-NtWriteFile
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As IntPtr, ByRef pbCancel As Boolean, ByVal dwCopyFlags As CopyFileFlags) As Long
Private Declare Function ReadFile Lib "kernel32" Alias "ReadFile" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Function WriteFile Lib "kernel32" Alias "WriteFile" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Function NtReadFile Lib "NTDLL.DLL" (ByVal Handle As Long, ByVal Events As Long, ByVal ApcRoutine As Long, ByVal ApcContext As Long, ByVal IoStatus As Long, ByVal Buffer As Long, ByVal Length As Long, Optional ByVal Number As Long, Optional ByVal Keys As Long) As Long
Private Declare Function NtWriteFile Lib "NTDLL.DLL" (ByVal Handle As Long, ByVal Events As Long, ByVal ApcRoutine As Long, ByVal ApcContext As Long, ByVal IoStatus As Long, ByVal Buffer As Long, ByVal Length As Long, Optional ByVal Number As Long, Optional ByVal Keys As Long) As Long
删除文件:
1.CreateFile 2.NtCreateFile 3.NtSetInformationFile 4.NtDeleteFile
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function NtCreateFile Lib "NTDLL.DLL" (ByRef FileHandle As Long, ByVal DesiredAccess As Long, ObjectAttributes As Any, IoStatusBlock As Any, Allocation As Long, ByVal FileAttributes As Long, ByVal ShareAccess As Long, ByVal CreateDisposition As Long, ByVal CreateOptions As Long, ByVal EaBuffer As Long, ByVal EaLength As Long) As Long
Private Declare Function NtSetInformationFile Lib "NTDLL.DLL" (ByVal FileHandle As Long, IoStatusBlock As IO_STATUS_BLOCK, FileInformation As Any, ByVal Length As Long, ByVal FileInformationClass As Long) As Long
Private Declare Function NtDeleteFile Lib "NTDLL.DLL" (ObjectAttributes As OBJECT_ATTRIBUTES) As Long
移动文件:
1.MoveFile 2.MoveFileEx 3.NtSetInformationFile
Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
重命名文件:
1.MoveFile 2.MoveFileEx 3.NtSetInformationFile
以上操作都通用:
SHFileOperation
Private Declare Function SHFileOperation Lib "shell32.dll" Alias " SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
创建、打开文件(夹):
1.CreateFile 2.NtCreateFile 3.CreateDirectory 4.CreateDirectoryEx
Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Private Declare Function CreateDirectoryEx Lib "kernel32" Alias "CreateDirectoryExA" (ByVal lpTemplateDirectory As String, ByVal lpNewDirectory As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
查看文件属性:
1.SHObjectProperties 2.ShellExecuteEx
Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef s As SHELLEXECUTEINFO) As Long
Private Declare Function SHObjectProperties Lib "shell32" Alias "#178" (ByVal hwndOwner As Long, ByVal uFlags As Long, ByVal lpstrName As String, ByVal lpstrPar As String) As Long
预先准备三个图标文件,用于树型控件中显示磁盘符号和文件夹的图像之用。
1、窗体上添加控件如下:
组合框控件 ComboBox1,树型控件 TreeView1,列表框控件 ListBox1,图像列表控件 ImageList1。
选中TreeView1,设置其ImageList属性为ImageList1。
2、设置属性
选中图像列表控件 ImageList1,在属性窗口里,选中属性Images,单击三个小点按钮,出现图像集合编辑器窗口,单击[添加按钮],一一把准备好的图标文件进行添加,注意先后次序,如果不符合要求可以通过上下移动按钮重新改变次序。完成后单击[确定]。
运行图如下:
完整代码如下:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'添加系统所有磁盘目录符号
For Each MyDrive As String In Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'显示第一个磁盘符号
ComboBox1.Text = ComboBox1.Items(0)
End Sub
'递归过程添加目录树
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim Mynode As New TreeNode
'先添加本目录
Mynode.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)
'为节点指定未被选中时显示的图标
Mynode.ImageIndex = 1
'为节点指定被选中时显示的图标
Mynode.SelectedImageIndex = 2
nodeFather.Nodes.Add(Mynode)
Application.DoEvents()
Try
Dim str() As String = Directory.GetDirectories(strPath)
'递归遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), Mynode)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Mynode = Nothing
End Sub
'根据给出的盘符添加目录树
Private Sub AddRootDirectory(ByVal DiscSymbol As String)
Dim Nynode As New TreeNode
'先把磁盘盘符添加到树中
TreeView1.Nodes.Clear()
Nynode.ImageIndex = 0
Nynode.Text = DiscSymbol
Nynode.SelectedImageIndex = -1
TreeView1.Nodes.Add(Nynode)
Dim i As Integer
'获取磁盘根目录下的文件夹
Dim str() As String = Directory.GetDirectories(DiscSymbol "\")
For i = 0 To str.GetUpperBound(0)
'调用递归过程遍历该文件夹里的所有子文件夹,并添加到树型控件
AddDirectory(DiscSymbol, str(i), Nynode)
Next
Nynode = Nothing
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'根据磁盘符号的变更,显示根目录里的文件
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根据磁盘符号的变更,重新显示目录树
Dim DiscSymbol As String
DiscSymbol = Microsoft.VisualBasic.Left(ComboBox1.Text, Len(ComboBox1.Text) - 1)
Call AddRootDirectory(DiscSymbol)
End Sub
'递归过程根据子目录寻找上级目录名--从而构成完整的目录路径
Private Sub AllPath(ByVal ThisNode As TreeNode, ByRef MyPathName As String)
If ThisNode.Level 1 Then
'该节点层数大于1,其父节点不是磁盘根目录
MyPathName = ThisNode.Parent.Text "\" MyPathName
Dim MyNode As TreeNode = ThisNode.Parent
Call AllPath(MyNode, MyPathName)
Else
'该节点层数等于1,其父节点就是磁盘根目录
MyPathName = ComboBox1.Text MyPathName
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
'为了搜索选中的节点对应目录的文件,需要组成全路径
Dim MyAllPathName As String = TreeView1.SelectedNode.Text
Dim MyNode As TreeNode = TreeView1.SelectedNode
If TreeView1.SelectedNode.Level = 0 Then
'如果选中的是根节点
MyAllPathName = ComboBox1.Text
Else
'如果选中的是非根节点,调用递归过程组成全路径
Call AllPath(MyNode, MyAllPathName)
MyAllPathName = MyAllPathName "\"
End If
'根据路径,搜索文件名并显示
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End Sub
End Class