代码:
Dim selectedItem As String = itemListBox.Items(itemListBox.SelectedIndex)
Mydp3.Text = My.Application.Info.DirectoryPath + ("\") + (selectedItem) + (".txt")
Dim a As String = My.Computer.FileSystem.ReadAllText(Mydp3.Text)
Dim b As String() = a.Split(vbNewLine)
ListBox2.Items.AddRange(b)
如果我从ListBox
中选择一项,则会收到非法字符路径错误。相反,如果我选择第一个项目,则不会出错。
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.GetFileName(String path)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path, Encoding encoding)
at System.IO.File.ReadAllText(String path, Encoding encoding)
at Microsoft.VisualBasic.MyServices.FileSystemProxy.ReadAllText(String file)
at Algoritm_Lottery_V4._1._1._3.Form1.itemListBox_SelectedIndexChanged(Object sender, EventArgs e) in C:\Users\xx\Desktop\Algoritm Lottery V3.4.4.9\Form1.vb:line 852
at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Algoritm_Lottery_V4._1._1._3.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()"
"mscorlib"
"Illegal characters in path."
我在项目列表框中有以下名称:
1
2
3
对于1:在listbox2中,您必须显示相应的元素,这将发生。 对于2和3,它不起作用。
每个文本文件1,2,3.txt中的项目如下
3.75
14
70
300
1400
6500
25000
125000
答案 0 :(得分:0)
使用IO> Path类来处理路径。 改变这个
Mydp3.Text = My.Application.Info.DirectoryPath + ("\") + (selectedItem) + (".txt")
到
selectedItem = IO.Path.Combine(My.Application.Info.DirectoryPath, selectedItem)
Mydp3.Text = IO.Path.ChangeExtension(selectedItem, "txt")
IO.path class具有一些您可能会发现有用的功能。
编辑:无法看到itemListBox,很难回答。修复 可能很简单,就像添加.ToString
Dim selectedItem As String = itemListBox.Items(itemListBox.SelectedIndex).ToString