如何修复此错误(请参阅消息) [英] How do I fix this error (see message)
本文介绍了如何修复此错误(请参阅消息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个控制台应用程序,我正在尝试获取Windows目录。
我有以下代码,但它说这是不平衡的堆。我很绝望。
私人 宣布 函数 GetWindowsDirectory Lib kernel32 别名 GetWindowsDirectoryA( ByVal lpBuffer As 字符串, ByVal nSize 作为 长) As Long
''' < 摘要 < span class =code-summarycomment>>
''' 返回Windows安装目录的路径。
''' < / summary >
''' < 返回 < span class =code-summarycomment>> < / returns >
''' < 备注 > < /备注 >
私人 功能 GetPath()作为 字符串
' 创建缓冲区[
GetPath = Space( 255 )
' ]
' 返回Windows目录路径[
调用 GetWindowsDirectory(GetPath,Len(GetPath))
' ]
结束 功能
在此先感谢,
iProgramIt
编辑:
完整代码更改登录屏幕壁纸:
公共 功能 ApplyStyles( ByVal EnableStyles 作为 Boolean , ByVal ImageToUse As String )
' 检查操作系统版本[
如果 不 My.Computer.Info.OSFullName.StartsWith( < span class =code-string> Microsoft Windows 7)然后
MessageBox.Show( 您必须运行Microsoft Windows 7才能使用此程序, 错误,MessageBoxButtons.OK,MessageBoxIcon。错误,MessageBoxDefaultButton.But ton1)
退出 功能
结束 如果
' ]
' 获取Windows目录[
< span class =code-keyword> Dim WindowsInstallPath As String
WindowsInstallPath = GetPath()。Trim()。TrimEnd(Chr( 0 ))& \
' ]
' 创建缺少文件夹[
如果 不 My.Computer.FileSystem.DirectoryExists(WindowsInstallPath & System32 \ ocobe \ info)然后
My.Computer.FileSystem.CreateDirectory(WindowsInstallPath& System32 \ ocobe \\ info)
结束 如果
如果 不 My.Computer.FileSystem.DirectoryExists(WindowsInstallPath& System32 \ ocobe \ info\backgrounds)然后
My.Computer.FileSystem.CreateDirectory(WindowsInstallPath& System32 \ ocobe \ info\backgrounds)
结束 如果
' ]
' 检查空白框[
如果 ImageToUse.Trim = 然后
' 要求选择[
Dim MyResult As DialogResult = MessageBox.Show ( 没有选择图片文件,你想现在选择一个吗?, No File Selected,MessageBoxButtons.YesNo,MessageBoxIcon.Warning)
' ]
' 确认[
< span class =code-keyword>选择 案例 MyResult
案例 Windows .Forms.DialogResult.Yes
结束 选择
' ]
' 退出方法[
退出 功能
' ]
结束 如果
' ]
' 检查文件存在[
如果 不 My.Computer.FileSystem.FileExists(ImageToUse)然后
' 显示错误并中止[
MessageBox.Show( 指定的图像文件不存在,请确保它存在于您的计算机上。 , 错误 - 找不到文件,MessageBoxButtons.OK,MessageBoxIcon。错误)
退出 功能
' ]
结束 如果
' ]
尝试
' 创建临时文件[
Dim TemporaryName As < span class =code-keyword> String = FileIO.FileSystem.GetTempFileName
' ]
' 重新格式化[
Dim ChangeToJPEG As 新位图(ImageToUse)
' ]
' 保存重新格式化的文件[
ChangeToJPEG.Save(TemporaryName,System.Drawing.Imaging.ImageFormat.Jpeg)
' ]
' 发布位图[
ChangeToJPEG.Dispose()
' ]
' 获取临时文件大小[
< span class =code-keyword> Dim MyInformation As 新 IO.FileInfo(TemporaryName )
如果 MyInformation.Length> 262144 然后
' 删除临时文件[
如果 MyInformation.Exists()然后
MyInformation.Delete()
结束 如果
' ]
' 错误[
MessageBox.Show( < span class =code-string>指定的图像文件在转换为JPG文件时将超过256KB且太大而无法使用。请选择另一个文件。, 错误 - 文件过大,MessageBoxButtons.OK ,MessageBoxIcon。错误)
退出 功能
' ]
结束 如果
' ]
' 删除临时文件[
如果 MyInformation.Exists()那么
MyInformation.Delete()
结束 如果
' ]
Catch MyException 为温泉n>例外
' 格式错误[
MessageBox.Show ( 指定的图像文件格式不正确。登录后台更改程序无法将其转换为JPG格式以用作登录屏幕。请选择另一个文件。, 错误 - 格式错误,MessageBoxButtons.OK, MessageBoxIcon。错误)
退出 功能
' ]
结束 尝试
' 启用或禁用后台[
Dim MyEnableSelection As 整数
如果 EnableStyles 那么
MyEnableSelection = 1
其他
MyEnableSelection = 0
结束 如果
My.Computer.Registry.SetValue( HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \ Windows \ CurrentVersion \Authentication\LogonUI \ Background, OEMBackground,MyEnableSelection)
' ]
' 最后更改屏幕[
尝试
' 重新格式化[
Dim MyFinalPath 作为 字符串 = WindowsInstallPat h& System32 \ ocobe \ info\backgrounds \ backgroundDefault.jpg
< span class =code-keyword> Dim ChangeToJPEG As 新位图(ImageToUse)
' ]
' 删除旧文件[
如果 My.Computer.FileSystem.FileExists (MyFinalPath)然后
My.Computer.FileSystem.DeleteFile(MyFinalPath)
结束 如果
' ]
' 保存重新格式化文件[
ChangeToJPEG.Save(MyFinalPath,System.Drawing.Imaging.ImageFormat.Jpeg)
' ]
' 发布位图[
ChangeToJPEG.Dispose()
' ]
设置包含路径的注册表文件[
My.Computer.Registry.SetValue( HKEY_LOCAL_MACHINE \ SOFTWARE \ LogonBackgroundChanger, PersistFile,ImageToUse)
' ]
Catch MyException As 异常
MessageBox.Show( 指定的图像文件格式不正确。登录后台更改程序无法将其转换为JPG格式以用作登录屏幕。请选择另一个文件。, 错误 - 格式错误,MessageBoxButtons.OK, MessageBoxIcon。错误)
退出 功能
结束 尝试
' ]
' 显示成功[
MessageBox.Show( 更改为已成功应用。, 已保存的更改,MessageBoxButtons.OK,MessageBoxIcon.Information)
' ]
返回 good
结束 < span class =code-keyword> Function
解决方案
或者......你可以问.NET .. 。
Console.WriteLine(Environment.GetEnvironmentVariable( WINDIR跨度>))
Hi all,
I have a console app and I am trying to get the windows dir.
I have the following code, but it says it is unbalancing the stack. I am desperate.
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
''' <summary>
''' Returns the path of the windows installation directory.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetPath() As String
'Create Buffer [
GetPath = Space(255)
']
'Return Windows Directory Path [
Call GetWindowsDirectory(GetPath, Len(GetPath))
']
End Function
Thanks in advance,
iProgramIt
Edit:
Full code for changing logon screen wallpaper:
Public Function ApplyStyles(ByVal EnableStyles As Boolean, ByVal ImageToUse As String)
'Check OS Version [
If Not My.Computer.Info.OSFullName.StartsWith("Microsoft Windows 7") Then
MessageBox.Show("You must be running Microsoft Windows 7 in order to use this program", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Exit Function
End If
']
'Get Windows Directory [
Dim WindowsInstallPath As String
WindowsInstallPath = GetPath().Trim().TrimEnd(Chr(0)) & "\"
']
'Create Missing Folders [
If Not My.Computer.FileSystem.DirectoryExists(WindowsInstallPath & "System32\oobe\info") Then
My.Computer.FileSystem.CreateDirectory(WindowsInstallPath & "System32\oobe\info")
End If
If Not My.Computer.FileSystem.DirectoryExists(WindowsInstallPath & "System32\oobe\info\backgrounds") Then
My.Computer.FileSystem.CreateDirectory(WindowsInstallPath & "System32\oobe\info\backgrounds")
End If
']
'Check For Blank Box [
If ImageToUse.Trim = "" Then
'Ask To Select [
Dim MyResult As DialogResult = MessageBox.Show("There is no picture file selected, would you like to select one now?", "No File Selected", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
']
'Confirmation [
Select Case MyResult
Case Windows.Forms.DialogResult.Yes
End Select
']
'Exit Method [
Exit Function
']
End If
']
'Check File Exists [
If Not My.Computer.FileSystem.FileExists(ImageToUse) Then
'Show Error And Abort [
MessageBox.Show("The image file specified does not exist, please ensure that it is present on your computer.", "Error - File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Function
']
End If
']
Try
'Create Temporary File [
Dim TemporaryName As String = FileIO.FileSystem.GetTempFileName
']
'Reformat [
Dim ChangeToJPEG As New Bitmap(ImageToUse)
']
'Save Reformatted File [
ChangeToJPEG.Save(TemporaryName, System.Drawing.Imaging.ImageFormat.Jpeg)
']
'Release Bitmap [
ChangeToJPEG.Dispose()
']
'Get Temporary File Size [
Dim MyInformation As New IO.FileInfo(TemporaryName)
If MyInformation.Length > 262144 Then
'Delete Temporary File [
If MyInformation.Exists() Then
MyInformation.Delete()
End If
']
'Error [
MessageBox.Show("The image file specified would be over 256KB when converted to a JPG file and too large to use. Please select another file.", "Error - File Too Large", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Function
']
End If
']
'Delete Temporary File [
If MyInformation.Exists() Then
MyInformation.Delete()
End If
']
Catch MyException As Exception
'Format Error [
MessageBox.Show("The image file specified is not of the correct format. Logon Background Changer cannot convert it to JPG format for use as a logon screen. Please select another file.", "Error - Incorrect Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Function
']
End Try
'Enable Or Disable Background [
Dim MyEnableSelection As Integer
If EnableStyles Then
MyEnableSelection = 1
Else
MyEnableSelection = 0
End If
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background", "OEMBackground", MyEnableSelection)
']
'Finally Change Screen [
Try
'Reformat [
Dim MyFinalPath As String = WindowsInstallPath & "System32\oobe\info\backgrounds\backgroundDefault.jpg"
Dim ChangeToJPEG As New Bitmap(ImageToUse)
']
'Delete Old File [
If My.Computer.FileSystem.FileExists(MyFinalPath) Then
My.Computer.FileSystem.DeleteFile(MyFinalPath)
End If
']
'Save Reformatted File [
ChangeToJPEG.Save(MyFinalPath, System.Drawing.Imaging.ImageFormat.Jpeg)
']
'Release Bitmap [
ChangeToJPEG.Dispose()
']
'Set Registry File Containing Path [
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\LogonBackgroundChanger", "PersistFile", ImageToUse)
']
Catch MyException As Exception
MessageBox.Show("The image file specified is not of the correct format. Logon Background Changer cannot convert it to JPG format for use as a logon screen. Please select another file.", "Error - Incorrect Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Function
End Try
']
'Show Success [
MessageBox.Show("The changes were successfully applied.", "Changes Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
']
Return "good"
End Function
解决方案
Or...you could just ask .NET...
Console.WriteLine(Environment.GetEnvironmentVariable("windir"))
这篇关于如何修复此错误(请参阅消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文