在visual basic中需要文件管理器的帮助 [英] Need help on a file manger in visual basic

查看:55
本文介绍了在visual basic中需要文件管理器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我刚刚创建了一个文件管理器,我希望能够添加新文件夹和新文件,如文本文件,所以我已经在我的项目中创建了一个记事本,我希望能够保存从我的记事本文件到文件管理器,并能够用我的记事本打开文件。
这是我的代码看起来像,我不知道如何为此编写代码。我正在使用visual basic 2013.谢谢


此代码是文件管理器。

 Public Class Filemanger 
Dim path As String
Dim nextpath As String


Private Sub Filemanger_Load(sender as Object,e As EventArgs)Handles MyBase.Load
On错误恢复下一个
path =" C:\"
每个我在My.Computer.FileSystem.GetDirectories(路径)

ListView1.Items.Add(I.Substring(I.LastIndexOf(" \")+ 1) ,ImageList1.Images.Count() - 2)

下一个

每个我在My.Computer.FileSystem.GetFiles(路径)
ListView1.Items。添加(I.Substring(I.LastIndexOf(" \")+ 1),ImageList1.Images.Count() - 1)

下一个
结束子

Private Sub ListView1_ItemSelectionChanged(sender As Object,e作为ListViewItemSelectionChangedEventArgs)处理ListView1.ItemSelectionChanged
nextpath = path +" \" + e.Item.Text
End Sub

Private Sub ListView1_MouseDoubleClick(sender As Object,e As MouseEventArgs)处理ListView1.MouseDoubleClick
On Error Resume Next
If( My.Computer.FileSystem.DirectoryExists(nextpath))然后
path = nextpath
ListView1.Clear()
TextBox1.Text = path

For Each I In My .Computer.FileSystem.GetDirectories(path)

ListView1.Items.Add(I.Substring(I.LastIndexOf(" \")+ 1),ImageList1.Images.Count() - 2)

下一个

每个我在My.Computer.FileSystem.GetFiles(路径)
ListView1.Items.Add(I.Substring(I.LastIndexOf) (" \")+ 1),ImageList1.Images.Count() - 1)
下一个

否则

MsgBox(" Cant open文件")

结束如果

结束次级

私有子ListView1_S electionsIndexChanged(sender As Object,e As EventArgs)Handles ListView1.SelectedIndexChanged

End Sub

Private Sub TextBox1_TextChanged(sender As Object,e As EventArgs)Handles TextBox1.TextChanged
path = TextBox1.Text

On Error Resume Next
If(My.Computer.FileSystem.DirectoryExists(path))Then

ListView1.Clear()


每个我在My.Computer.FileSystem.GetDirectories(路径)

ListView1.Items.Add(I.Substring(I.LastIndexOf(") \")+ 1),ImageList1.Images.Count() - 2)

下一个

每个我在My.Computer.FileSystem.GetFiles(路径)
ListView1.Items.Add(I.Substring(I.LastIndexOf(" \")+ 1),ImageList1.Images.Count() - 1)
Next

Else

MsgBox(" Cant open file")

End if
End Sub
结束等级

此代码是我的记事本

 Public Class NotePad 

Private Sub NewToolStripMenuItem_Click(sender As Object,e As EventArgs)Handles NewToolStripMenuItem.Click
RichTextBox1.Clear()
End Sub

Private Sub OpenToolStripMenuItem_Click(sender as Object,e As EventArgs)处理OpenToolStripMenuItem.Click
Dim dlg As OpenFileDialog = New OpenFileDialog
dlg.Title =" Open"
dlg.Filter =" Rich Text Files(* .rtf)| * .rtf"
如果dlg.ShowDialog()= System.Windows.Forms.DialogResult.OK然后

RichTextBox1.LoadFile(dlg.FileName)
结束如果
结束Sub

Private Sub SaveToolStripMenuItem_Click(sender As Object,e As EventArgs)处理SaveToolStripMenuItem.Click
Dim dlg As SaveFileDialog = New SaveFileDialog
dlg.Title =" Save"
dlg.Filter =" Rich Text Files(* .rtf)| * .rtf"
如果dlg.ShowDialog()= System.Windows.Forms.DialogResult.OK然后
RichTextBox1.SaveFile(dlg.FileName,
RichTextBoxStreamType.RichText)
结束如果
End Sub

Private Sub ExitToolStripMenuItem_Click(sender As Object,e As EventArgs)处理ExitToolStripMenuItem.Click
End
End Sub

Private Sub UndoToolStripMenuItem_Click( sender As Object,e As EventArgs)Handles UndoToolStripMenuItem.Click
RichTextBox1.Undo()
End Sub

Private Sub RedoToolStripMenuItem_Click(sender as Object,e As EventArgs)处理RedoToolStripMenuItem。点击
RichTextBox1.Redo()
End Sub

Private Sub CutToolStripMenuItem_Click(sender As Object,e As EventArgs)Handles CutToolStripMenuItem.Click
RichTextBox1.Cut()
End Sub

Private Sub CopyToolStripMenuItem_Click(sender As Object,e As EventArgs)处理CopyTo olStripMenuItem.Click
RichTextBox1.Copy()

End Sub

Private Sub PastToolStripMenuItem_Click(sender As Object,e As EventArgs)Handles PastToolStripMenuItem.Click
RichTextBox1.Paste()

End Sub

Private Sub CToolStripMenuItem_Click(sender As Object,e As EventArgs)处理CToolStripMenuItem.Click
RichTextBox1.Clear()
End Sub

Private Sub SelectAllToolStripMenuItem_Click(sender As Object,e As EventArgs)Handles SelectAllToolStripMenuItem.Click
RichTextBox1.SelectAll()
End Sub

Private Sub FontToolStripMenuItem_Click(sender As Object,e As EventArgs)Handles FontToolStripMenuItem.Click
Dim dlg As FontDialog = New FontDialog
dlg.Font = RichTextBox1.Font
if dlg.ShowDialog = System。 Windows.Forms.DialogResult.OK然后
RichTextBox1.Font = dlg.Font
结束如果
结束Sub

Private Sub ColorToolStripMenuItem_Click(sender As Object,e As EventArgs)处理ColorToolStripMenuItem.Click
Dim dlg As ColorDialog = New ColorDialog
dlg.Color = RichTextBox1.ForeColor
if dlg。 ShowDialog = System.Windows.Forms.DialogResult.OK然后
RichTextBox1.ForeColor = dlg.Color
End if
End Sub
End Class


$





解决方案


您好,我刚刚创建了一个文件管理器,我希望能够添加新文件夹和新文件,如文本文件,所以我在我的项目中制作了一个记事本,我希望能够将文件从我的记事本保存到文件管理器,并能够用我的记事本打开文件。


不要使用On Error,也不要使用'+'进行字符串连接 - 这两种做法都会产生很难识别和修复的问题。


你的文件管理器类不提供记事本应用程序所需的内容,以便能够将命名文件保存到选定的文件夹。 在目前的格式中,无法做到这一点。


Filemanger类中的代码应该是对话框形式。该表单还将包含列表框和文本框。 目前,您有一个类,其中包含引用不属于该类的控件的代码,但不起作用。 
文件夹和文件应列在单独的列表框中,因为您将对文件的单击响应与单击文件夹完全不同。


获取文件夹的代码列表框中的内容和显示应采用自己的方法,因为您会在更改内容时调用该方法。  方法的参数将是要显示的文件夹的名称。 该类将
有一个属性'StartFolder'(可能是空白的),它将用于代替文字'path =" C:\"'来设置将在列出的时候列出的初始文件夹class被实例化。 这是一次你将调用文件/文件夹显示
例程。当用户点击文件夹列表框中的文件夹时,您也会调用它,参数将是用户点击的文件夹名称。  您需要知道该文件夹的完整路径才能获取文件等,但您只需显示文件夹名称
,这表示您需要提供值变量的内容以及显示变量as文件夹列表框的数据源,例如List(Of String,String)。 文件列表框不需要
,因为文件的完整路径始终与当前显示的文件夹相同。您还需要一种机制,用户可以指示"返回一级" - 可能是文件夹列表框顶部附近的按钮。


或者,您可以使用内置文件打开/文件保存对话框。

http://www.techrepublic.com/blog/software-engineer/let-users-pick-a-file-to-open-via-openfiledialog-in-vbnet/


Hello, I just made a file manger, and I want to be able to add new folders and new files like text file, all so I have made a notepad in my project and I want to be able to save file from my notepad to the file manger and be able to open files with my notepad. Here is what my code looks like, I dont know how to write code for that. I'm working with visual basic 2013. Thank You

This code is the file manger.

Public Class Filemanger
    Dim path As String
    Dim nextpath As String


    Private Sub Filemanger_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        On Error Resume Next
        path = "C:\"
        For Each I In My.Computer.FileSystem.GetDirectories(path)

            ListView1.Items.Add(I.Substring(I.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

        Next

        For Each I In My.Computer.FileSystem.GetFiles(path)
            ListView1.Items.Add(I.Substring(I.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)

        Next
    End Sub

    Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
        nextpath = path + "\" + e.Item.Text
    End Sub

    Private Sub ListView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDoubleClick
        On Error Resume Next
        If (My.Computer.FileSystem.DirectoryExists(nextpath)) Then
            path = nextpath
            ListView1.Clear()
            TextBox1.Text = path

            For Each I In My.Computer.FileSystem.GetDirectories(path)

                ListView1.Items.Add(I.Substring(I.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

            Next

            For Each I In My.Computer.FileSystem.GetFiles(path)
                ListView1.Items.Add(I.Substring(I.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)
            Next

        Else

            MsgBox("Cant open file")

        End If

    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        path = TextBox1.Text

        On Error Resume Next
        If (My.Computer.FileSystem.DirectoryExists(path)) Then

            ListView1.Clear()


            For Each I In My.Computer.FileSystem.GetDirectories(path)

                ListView1.Items.Add(I.Substring(I.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

            Next

            For Each I In My.Computer.FileSystem.GetFiles(path)
                ListView1.Items.Add(I.Substring(I.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)
            Next

        Else

            MsgBox("Cant open file")

        End If
    End Sub
End Class

This code is my notepad

Public Class NotePad

    Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        Dim dlg As OpenFileDialog = New OpenFileDialog
        dlg.Title = "Open"
        dlg.Filter = "Rich Text Files (*.rtf)|*.rtf"
        If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            RichTextBox1.LoadFile(dlg.FileName)
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        Dim dlg As SaveFileDialog = New SaveFileDialog
        dlg.Title = "Save"
        dlg.Filter = "Rich Text Files (*.rtf)|*.rtf"
        If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            RichTextBox1.SaveFile(dlg.FileName,
            RichTextBoxStreamType.RichText)
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
        RichTextBox1.Undo()
    End Sub

    Private Sub RedoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RedoToolStripMenuItem.Click
        RichTextBox1.Redo()
    End Sub

    Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
        RichTextBox1.Copy()

    End Sub

    Private Sub PastToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PastToolStripMenuItem.Click
        RichTextBox1.Paste()

    End Sub

    Private Sub CToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CToolStripMenuItem.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectAllToolStripMenuItem.Click
        RichTextBox1.SelectAll()
    End Sub

    Private Sub FontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FontToolStripMenuItem.Click
        Dim dlg As FontDialog = New FontDialog
        dlg.Font = RichTextBox1.Font
        If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            RichTextBox1.Font = dlg.Font
        End If
    End Sub

    Private Sub ColorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ColorToolStripMenuItem.Click
        Dim dlg As ColorDialog = New ColorDialog
        dlg.Color = RichTextBox1.ForeColor
        If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            RichTextBox1.ForeColor = dlg.Color
        End If
    End Sub
End Class


解决方案

Hello, I just made a file manger, and I want to be able to add new folders and new files like text file, all so I have made a notepad in my project and I want to be able to save file from my notepad to the file manger and be able to open files with my notepad.

Don't use On Error, and don't use '+' for string concatenation - both practices will create problems that can be hard to identify and fix.

Your file manager class doesn't provide what the notepad application needs in order to be able to save a named file to a selected folder.  In its current format it can't be made to do that.

The code you have in the Filemanger class should be in a dialog form. That form will also include the listbox and text box.  Currently you have a class that includes code that refers to controls that aren't part of the class, and that won't work.  Folders and files should be listed in separate listboxes, because you will respond to a click on a file quite differently than a click on a folder.

The code to get the folder contents and display in the list boxes should be in its own method, because you will call that method when something changes.  The argument to the method will be the name of the folder to display.  The class will have a property 'StartFolder' (which could be blank) which will be used instead of the literal 'path = "C:\"' to set the initial folder that will be listed when the class is instantiated.  That's one time that you will call the File/Folder display routine. You will call it also when the user clicks on a folder in the folder list box, and the argument will be the folder name that the user clicked on.   You will need to know the full path to the folder in order to get files etc, but you are only displaying the folder name, so that suggests you need something that provides a value variable as well as a display variable as the data source for the folder listbox, such as a List(Of string, String).  The files list box doesn't need that because the full path to the files is always the same as the folder that is currently being displayed. You will also need a mechanism where the user can indicate 'Go back one level' - probably a button near the top of the folder listbox.

Or, you could use the built-in file open / file save dialogs.
http://www.techrepublic.com/blog/software-engineer/let-users-pick-a-file-to-open-via-openfiledialog-in-vbnet/


这篇关于在visual basic中需要文件管理器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆