- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到“路径中的非法字符”异常,但字符串似乎没有任何非法字符。
我该如何解决这个问题?:
Private Sub Button_Send_Click(sender As Object, e As EventArgs) Handles Button_Send.Click
For Each item As ListViewItem In ListView1.Items
Clipboard.SetText(item.SubItems(0).Text)
' First I copy the text to the clipboard to ensure in Explorer.exe if all is ok and also ifthe file exists... and yes, it exists.
' The example text is this:
' C:\Electro\Nueva carpeta\Aggresivnes - Dance Or Die.mp3
' (without any quotes)
' ...But this throws an "not exists":
If IO.File.Exists(item.SubItems(0).Text) Then MsgBox("exists") Else MsgBox("not exists")
' And here throws an exception of "*Illegal characters in path*" :
Dim File As New IO.FileInfo(item.SubItems(0).Text)
Next
End Su
UPDATE 2
我在自己的代码中犯了一个错误,没有注意到当没有问题时返回 true 而当问题存在时返回 false,所以我决定删除我的旧更新并编辑这个:
我做了一个函数来(再次)验证文件名转换或字符串变量中的某些内容是否有任何无效字符,遵循@Jim Mischel 回答指示:
#Region " Validate Windows FileName "
' [ Validate Windows FileName Function ]
'
' By Elektro H@cker
'
' Examples :
' MsgBox(Validate_Windows_FileName("C:\Test.txt")) ' Result: True
' MsgBox(Validate_Windows_FileName("C:\Te|st.txt")) ' Result: False
Private Function Validate_Windows_FileName(ByRef FileName As String)
Dim Directory As String = Nothing
Dim File As String = Nothing
Try
Directory = FileName.Substring(0, FileName.LastIndexOf("\")) & "\"
File = FileName.Split("\").Last
Catch
If Directory Is Nothing Then File = FileName
End Try
If Directory Is Nothing AndAlso File Is Nothing Then Return False
If Not Directory Is Nothing Then
For Each InvalidCharacter As Char In IO.Path.GetInvalidPathChars
If Directory.Contains(InvalidCharacter) Then
' MsgBox(InvalidCharacter)
Return False
End If
Next
End If
If Not File Is Nothing Then
For Each InvalidCharacter As Char In IO.Path.GetInvalidFileNameChars
If File.Contains(InvalidCharacter) Then
' MsgBox(InvalidCharacter)
Return False
End If
Next
End If
Return True ' FileName is valid
End Function
#End Region
好吧...现在我使用该函数再次确保完整路径/文件名和结果为“false”,这意味着该字符串包含无效字符,而该字符类似于“空格”。
MsgBox(Validate_Windows_FileName(item.SubItems(0).Text))
我不知道如何解决这个问题。
如果你想看完整的类(class),这里是:
Public Class Main
Dim WinAmpTitle As String = String.Empty
Dim WinAmpFile As String = String.Empty
Dim Sendto_Path As String
Dim WithEvents WinAmp_Timer As New Timer With {.Interval = 25, .Enabled = True}
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Nothing to do here at the momento...
End Sub
Private Sub WinAmp_Timer_Tick(sender As Object, e As EventArgs) Handles WinAmp_Timer.Tick
WinAmpTitle = WinAmpInfo.Title
WinAmpFile = WinAmpInfo.FileName
If Not TextBox_Title.Text = WinAmpTitle Then TextBox_Title.Text = WinAmpTitle
If Not TextBox_Filename.Text = WinAmpFile Then TextBox_Filename.Text = WinAmpFile
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button_Copy.Click
Dim item = ListView1.Items.Add(WinAmpFile)
item.SubItems.Add(ComboBox_Sendto.Text)
End Sub
Private Sub Button_Send_Click(sender As Object, e As EventArgs) Handles Button_Send.Click
For Each item As ListViewItem In ListView1.Items
Clipboard.SetText(item.SubItems(0).Text)
' First I copy the text to the clipboard to ensure in Explorer.exe if the file exists... and yes, it exists.
' The example text is this:
' C:\Electro\Nueva carpeta\Aggresivnes - Dance Or Die.mp3
' (without any quotes)
' ...But this throws an "not exists":
If IO.File.Exists(item.SubItems(0).Text) Then MsgBox("exists") Else MsgBox("not exists")
MsgBox(Validate_Windows_FileName(item.SubItems(0).Text)) ' Result: False (any invalid character)
' Here, an exception: "Illegal characters in path" :
Dim File As New IO.FileInfo(item.SubItems(0).Text)
' If item.SubItems(1).Text.ToLower = "electro" Then Sendto_Path = "C:\Electro"
' If item.SubItems(1).Text.ToLower = "techno" Then Sendto_Path = "C:\Techno"
' If item.SubItems(1).Text.ToLower = "trance" Then Sendto_Path = "C:\Trance"
'IO.File.Copy(File.FullName, IO.Path.Combine(Sendto_Path, File.Name))
Next
End Sub
#Region " Validate Windows FileName "
' [ Validate Windows FileName Function ]
'
' By Elektro H@cker
'
' Examples :
' MsgBox(Validate_Windows_FileName("C:\Test.txt")) ' Result: True
' MsgBox(Validate_Windows_FileName("C:\Te|st.txt")) ' Result: False
Private Function Validate_Windows_FileName(ByRef FileName As String)
Dim Directory As String = Nothing
Dim File As String = Nothing
Try
Directory = FileName.Substring(0, FileName.LastIndexOf("\")) & "\"
File = FileName.Split("\").Last
Catch
If Directory Is Nothing Then File = FileName
End Try
If Directory Is Nothing AndAlso File Is Nothing Then Return False
If Not Directory Is Nothing Then
For Each InvalidCharacter As Char In IO.Path.GetInvalidPathChars
If Directory.Contains(InvalidCharacter) Then
' MsgBox(InvalidCharacter)
Return False
End If
Next
End If
If Not File Is Nothing Then
For Each InvalidCharacter As Char In IO.Path.GetInvalidFileNameChars
If File.Contains(InvalidCharacter) Then
' MsgBox(InvalidCharacter)
Return False
End If
Next
End If
Return True ' FileName is valid
End Function
#End Region
End Class
...第二部分(我认为不太重要):
#Region " WinAmp Info "
' [ WinAmp Info Functions ]
'
' // By Elektro H@cker
'
' Examples:
' MsgBox(WinAmpInfo.Title) ' Result: Artist - Title
' MsgBox(WinAmpInfo.FileName) ' Result: C:\Title.ext
Public Class WinAmpInfo
Private Const WinampClassName As String = "Winamp v1.x"
Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Byte, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Shared Function Title() As String
Dim hwnd As IntPtr = FindWindow(WinampClassName, vbNullString)
Dim lpText As String = String.Empty
Dim strTitle As String = String.Empty
Dim intLength As Integer = 0
Dim intName As Integer = 0
Dim intLeft As Integer = 0
Dim intRight As Integer = 0
Dim intDot As Integer = 0
If hwnd.Equals(IntPtr.Zero) Then Return "WinAmp is not running"
lpText = New String(Chr(0), 100)
intLength = GetWindowText(hwnd, lpText, lpText.Length)
If (intLength <= 0) _
OrElse (intLength > lpText.Length) _
Then Return "Unknown"
strTitle = lpText.Substring(0, intLength)
intName = strTitle.IndexOf(" - Winamp")
intLeft = strTitle.IndexOf("[")
intRight = strTitle.IndexOf("]")
If (intName >= 0) _
AndAlso (intLeft >= 0) _
AndAlso (intName < intLeft) _
AndAlso (intRight >= 0) _
AndAlso (intLeft + 1 < intRight) _
Then Return strTitle.Substring(intLeft + 1, intRight - intLeft - 1)
If (strTitle.EndsWith(" - Winamp")) _
AndAlso (strTitle.Length > " - Winamp".Length) _
Then strTitle = strTitle.Substring(0, strTitle.Length - " - Winamp".Length)
intDot = strTitle.IndexOf(".")
If (intDot > 0) _
AndAlso (IsNumeric(strTitle.Substring(0, intDot))) _
Then strTitle = strTitle.Remove(0, intDot + 1)
Return strTitle.Trim
End Function
Public Shared Function FileName() As String
Dim lp As Long, lpWinamp As Long, iIndex As Long, PID As Long, bRet As Long, dwRead As Long
Dim Buffer(260) As Byte
Dim hWndWinamp As IntPtr = FindWindow(WinampClassName, vbNullString)
If hWndWinamp = 0 Then Return Nothing
iIndex = SendMessage(hWndWinamp, &H400, 0, 125)
lp = SendMessage(hWndWinamp, &H400, iIndex, 211)
If lp = 0 Then Return Nothing
Call GetWindowThreadProcessId(hWndWinamp, PID)
lpWinamp = OpenProcess(&H10, 0, PID)
If lpWinamp = 0 Then Return Nothing
bRet = ReadProcessMemory(lpWinamp, lp, Buffer(0), 260, dwRead)
Call CloseHandle(lpWinamp)
Return System.Text.UnicodeEncoding.Default.GetString(Buffer)
End Function
End Class
#End Region
UPDATE 3:
我的新尝试......一切都解释清楚了,我得到了一个无效的字符,我不知道为什么......
Private Sub Button_Send_Click(sender As Object, e As EventArgs) Handles Button_Send.Click
For Each item As ListViewItem In ListView1.Items
Dim filenameee As String = item.SubItems(0).Text.Trim ' The trim is...fuck, is just 'cause I don't know what more try to get a valid path...)
Clipboard.SetText(filenameee) ' result: "C:\Test.mp3" (Without any double quote of course)
' ...but this launchs an "not exists" msgbox with the filename "C:\Test.mp3":
If IO.File.Exists(filenameee) Then MsgBox("exists") Else MsgBox("not exists")
MsgBox(filenameee) ' this showns "C:\Test.mp3" /without double quotes)
MsgBox("filename is vlaid?:" & Validate_Windows_FileName(filenameee)) ' Result: False (path is invalid) (REALLY!!!!??? WTF)
' Here, an exception: "Illegal characters in path" :
Dim File As New IO.FileInfo(item.SubItems(0).Text) ' (REALLY!!!!???)
Next
End Sub
UPDATE 4:
照片:
应用本身:
(C:\Test.mp3为filenameee变量文本)
Io.file.exists 证明:
显示filenameee变量内容:
检查文件名是否有效:
显示文件名中假定的非法字符:
(空格?)
异常:
UPDATE 5
我认为真正的问题在于我在第 2 次更新中发布的 winamp 类的“filename()”函数:
Return System.Text.UnicodeEncoding.Default.GetString(Buffer)
因为它似乎返回了一个有效的字符串,正如您在图像中看到的那样,但在“Autos”中似乎是一个大字符串:
如果有人可以帮我解决这个问题...
谢谢。
UPDATE 6:
这是有效的,我想知道是否存在一种方法或其他方法来改进它以避免使用 For 循环:
Dim i As Int32 = 0
For Each by In System.Text.Encoding.Default.GetString(Buffer)
If by = Nothing Then Exit For 'MsgBox(i)
i += 1
Next
Return System.Text.Encoding.Default.GetString(Buffer, 0, i)
最佳答案
根据无效路径字符 (GetInvalidPathChars) 和无效文件名字符 (GetInvalidFilenameChars) 检查您的文件名。这应该会告诉您哪些字符不好。
请注意 File.Exists
不会“抛出”任何东西。它返回 false
表示该文件不存在。作为the documentation状态:
The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Exists returns false. To check whether the path contains any invalid characters, you can call the GetInvalidPathChars method to retrieve the characters that are invalid for the file system. You can also create a regular expression to test the whether the path is valid for your environment.
换句话说,File.Exists
不在乎你传递给它的是不是垃圾。它会告诉您不存在具有该垃圾名称的文件。它不会告诉您您为它指定了错误的路径名。
我不知道你在那里用 ReadProcessMemory
等做什么,但你的问题可能出在这段代码上:
bRet = ReadProcessMemory(lpWinamp, lp, Buffer(0), 260, dwRead)
Call CloseHandle(lpWinamp)
Return System.Text.UnicodeEncoding.Default.GetString(Buffer)
在这里,您将数据读入一个 260 字节的缓冲区,然后创建一个字符串,但您没有告诉 GetString
要使用该缓冲区的多少字节,因此它将使用 (或尝试使用)所有这些。所以你最终会在你的字符串末尾得到各种各样的垃圾(可能)。
此外,我不清楚您是要尝试使用 Unicode 编码还是默认编码。您有 System.Text.UnicodeEncoding.Default
。这将为您提供 Default 的编码代码页,这是系统默认的ANSI代码页。如果您想要默认编码,则只需使用 System.Text.Encoding.Default
。
假设您需要以 null 结尾的 ANSI 字符串,那么您需要让代码在调用 GetString
之前找到字符串的长度。通过在缓冲区中搜索第一个 0 字节来做到这一点。这会给你长度,然后你可以调用GetString(buffer, 0, length) .
要找到长度,请执行以下操作:
Dim Len as Integer
For Len = 0 to 260
if Buffer(Len) = 0 Then Exit For
Next
Return System.Text.Encoding.Default.GetString(Buffer, 0, Len)
关于.net - IO.FileInfo 奇怪的异常 "Illegal characters in path"当有任何非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17349835/
#include #include using namespace std; //void multiply(int b); int main() { float total = 0; flo
我正在尝试加载存储在 HDFS 中的 Hadoop 集群上的 informatica 日志文件。我在 Python 中使用 subprocess 来执行此操作,但相信由于文件名而出现错误,我不确定如何
我尝试制作用于 Symfony 2 上传的 ajax 脚本。Chrome 返回此错误: Uncaught TypeError: Illegal invocation jquery.min.js:4 我
这只是一个简单的代码,用于检查一个数字是否是质数,我不知道这是编译器还是代码的问题。如果您能提供一些帮助,我们将不胜感激。 这是代码: #include "stdio.h" int main() {
当我运行这个 bash 脚本时: if [ [$EUID -ne 0] ]; then echo "This script must be run as root" 1>&2 exit 1
我最近偶然发现了 this jcstress 中的示例: @JCStressTest @State @Outcome(id = "10", expect =
我不理解这个错误(C2100:非法间接)。我标记了三个实例——都在底部附近。我在网上看过,我知道这与我的指示有关,但在 8 小时后,我完全迷路了。这里可能还有其他一些错误,但我什至无法分辨,因为我无法
我们正在运行一个 Create React App (CRA) Web 应用程序,我们已向其中添加了 Google Analytics v4。我们使用 ga-4-react 启动分析npm 包。 in
我想将多个图像对象发布到 testphp.php。但控制台打印错误说非法调用。 我已经尝试过: submit $("#sub").click(function(){ // get th
当文本框为空时尝试禁用按钮时,我在 google chrome 控制台中收到此错误: function isEmpty() { var r = document.getElementById;
我只是在玩 timesheet.js的源代码(参见具体行)并遇到一个小错误,请参见下面的代码行: '', 当我生成 HTML 并将其分成两行时,一切正常,但如果我执行以下操作将
public class Flatten { public static int[] flatten(int[][] x) { int totalLength = 0;
我的计算机上有一个 python3 脚本,我可以使用 python3motion_detection.py 运行它,并且该脚本有效,我尝试在我的 Raspberry 上使用它,但失败并显示消息非法指令
我正在尝试最近学到的一些多线程概念,但无法运行它。 它给出了 IllegalMonitorStateException 但没有弄清楚错误发生的原因。 因此,关于代码2线程引用名称填充器和写入器都共享一
这是 html 这是脚本 $('#submit').click(function() { var files = $("[type='file']")[0].fil
我尝试通过ajax提交表单,下面是表单。 Date Upload File
我几天前买了 C++ Primer 这本书,我开始学习这门新语言了! 此刻,我想弄明白为什么我自己写的这个 block 是非法的: 我将一个 const int 初始化为 512;我初始化一个指向 n
我收到以下错误消息 [Err] 1267 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,C
我正在尝试使用本教程创建一个 PostgreSQL 数据库: http://tutorials.jumpstartlab.com/topics/environment/environment.html
我想弄乱 Speech Recognition API,所以我从简单的页面开始,该页面在单击 body 元素时开始识别。我的 scripts.js 文件是: var recognition = new
我是一名优秀的程序员,十分优秀!