Dim Path as string =System.Reflection.Assembly.GetExecutingAssembly().Location
Path = System.IO.Path.GetDirectoryName(Path) & "\"
■ドライブ名取得(ルートディレクトリ)
コード:System.IO.Path.GetPathRoot("C:\test\sample.jpg")
結 果:C:\
■ディレクトリ名取得
コード: System.IO.Path.GetDirectoryName( "C:\test\sample.jpg")
結 果:C:\test
■拡張子の取得
コード:System.IO.Path.GetExtension("C:\test\sample.jpg")
結 果:.jpg
■ファイル名の取得
コード:System.IO.Path.GetFileName("C:\test\sample.jpg")
結 果:sample.jpg
■ファイル名の取得(拡張子除く)
コード:System.IO.Path.GetFileNameWithoutExtension("C:\test\sample.jpg")
結 果:sample
■ファイル有無
If System.IO.File.Exists("C:\temp\test.pdf") Then
End If
■ディレクトリ有無
If System.IO.Directory.Exists("C:\temp") Then
End If
Sub FolderCreate(SPath)
Dim KpDt2Arr() As String = SPath.Split("\"c)
Dim Lpi As Integer = 0
Dim GPath As String = ""
If SPath.StartsWith("\\") Then
For Each fldr As String In KpDt2Arr
If fldr & "" <> "" Then
If Lpi = 2 Then GPath &= "\\"
GPath &= fldr & "\"
If Lpi > 2 Then If System.IO.Directory.Exists(GPath) Then Else System.IO.Directory.CreateDirectory(GPath)
End If
Lpi += 1
Next
Else
For Each fldr As String In KpDt2Arr
If fldr & "" <> "" Then
GPath &= fldr & "\"
If Lpi > 0 Then If System.IO.Directory.Exists(GPath) Then Else System.IO.Directory.CreateDirectory(GPath)
End If
Lpi += 1
Next
End If
End Sub
■ファイルをコピーする(強制上書き)
System.IO.File.Copy("C:\temp\test.pdf","C:\temp\test2.pdf",True)
※ ,True とすると 強制上書きです。これを入れないと上書きエラーとなります。
Sub DeleteFile(ByVal stFilePath As String)
Dim cFileInfo As New System.IO.FileInfo(stFilePath)
If cFileInfo.Exists Then
If (cFileInfo.Attributes And System.IO.FileAttributes.ReadOnly) = System.IO.FileAttributes.ReadOnly Then
cFileInfo.Attributes = System.IO.FileAttributes.Normal
End If
cFileInfo.Delete()
End If
End Sub
Sub ShortCutLnk(StrA, StrB)
Dim shortcutPath As String = System.IO.Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), StrB)
'ショートカットのリンク先
Dim targetPath As String = StrA 'Application.ExecutablePath
'既存のショートカット削除
If System.IO.File.Exists(System.IO.Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), "ショートカット名.lnk")) Then
System.IO.File.Delete(System.IO.Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), "ショートカット名.lnk"))
End If
'WshShellを作成
Dim t As Type = Type.GetTypeFromCLSID(New Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"))
Dim shell = Activator.CreateInstance(t)
'WshShortcutを作成
Dim shortcut = shell.CreateShortcut(shortcutPath)
Dim DefaultPath As String = StrA
'リンク先
shortcut.TargetPath = DefaultPath
'作業フォルダ
shortcut.WorkingDirectory = DefaultPath
'アイコンのパス
'shortcut.IconLocation = Application.ExecutablePath + ",0"
'ショートカットを作成
shortcut.Save()
'後始末
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell)
End Sub
Sub CopyDirectory(ByVal sourceDirName As String, ByVal destDirName As String)
'コピー先のディレクトリがないときは作る
If Not System.IO.Directory.Exists(destDirName) Then
System.IO.Directory.CreateDirectory(destDirName)
'属性もコピー
System.IO.File.SetAttributes(destDirName,
System.IO.File.GetAttributes(sourceDirName))
End If
'コピー先のディレクトリ名の末尾に"\"をつける
If destDirName.Chars((destDirName.Length - 1)) <>
System.IO.Path.DirectorySeparatorChar Then
destDirName = destDirName + System.IO.Path.DirectorySeparatorChar
End If
'コピー元のディレクトリにあるファイルをコピー
Dim fs As String() = System.IO.Directory.GetFiles(sourceDirName)
Dim f As String
For Each f In fs
System.IO.File.Copy(f,
destDirName + System.IO.Path.GetFileName(f), True)
Next
'コピー元のディレクトリにあるディレクトリをコピー
Dim dirs As String() = System.IO.Directory.GetDirectories(sourceDirName)
Dim dir As String
For Each dir In dirs
CopyDirectory(dir, destDirName + System.IO.Path.GetFileName(dir))
Next
End Sub
Sub FolderHide(StrA)
Dim di As New System.IO.DirectoryInfo(StrA)
di.Attributes = di.Attributes Or System.IO.FileAttributes.Hidden
End Sub
'FileUpload("C:\ファイル名.拡張子", "ftp://ファイル名.拡張子", "ログインID", "パスワード")
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Sub FileUpload(strA, strB, strC, strD)
Dim u As New Uri(strB)
Dim uid As String = strC & ""
Dim upw As String = strD & ""
Dim ftpReq As System.Net.FtpWebRequest = CType(System.Net.WebRequest.Create(u), System.Net.FtpWebRequest)
ftpReq.Credentials = New System.Net.NetworkCredential(uid, upw)
ftpReq.Method = System.Net.WebRequestMethods.Ftp.UploadFile
ftpReq.KeepAlive = False
ftpReq.UseBinary = True
ftpReq.Timeout = 30000
ftpReq.UsePassive = True
'SSL通信
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf OnRemoteCertificateValidationCallback)
ftpReq.EnableSsl = True
ftpReq.Proxy = Nothing 'プロキシー無効対策
Dim reqStrm As System.IO.Stream = ftpReq.GetRequestStream()
Dim fs As New System.IO.FileStream(strA, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim buffer(1023) As Byte
While True
Dim readSize As Integer = fs.Read(buffer, 0, buffer.Length)
If readSize = 0 Then
Exit While
End If
reqStrm.Write(buffer, 0, readSize)
End While
fs.Close()
reqStrm.Close()
GC.Collect()
End Sub
Private Function OnRemoteCertificateValidationCallback(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
' 「SSL証明書の使用は問題なし」と示す
Return True
End Function
'FileDownload("C:\ファイル名.拡張子", "ftp://ファイル名.拡張子", "ログインID", "パスワード")
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
'ファイルダウンロード
Sub FileDownload(strA, strB, strC, strD)
Dim u As New Uri(strB)
Dim uid As String = strC & ""
Dim upw As String = strD & ""
Dim downFile As String = strA
Dim ftpReq As System.Net.FtpWebRequest = CType(System.Net.WebRequest.Create(u), System.Net.FtpWebRequest)
ftpReq.Credentials = New System.Net.NetworkCredential(uid, upw)
ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
ftpReq.KeepAlive = False
ftpReq.UseBinary = True
ftpReq.Timeout = 30000
ftpReq.UsePassive = True
ftpReq.Proxy = Nothing
'SSL通信
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf OnRemoteCertificateValidationCallback)
ftpReq.EnableSsl = True
Dim ftpRes As System.Net.FtpWebResponse = CType(ftpReq.GetResponse(), System.Net.FtpWebResponse)
Dim resStrm As System.IO.Stream = ftpRes.GetResponseStream()
Dim fs As New System.IO.FileStream(downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim buffer(1023) As Byte
While True
Dim readSize As Integer = resStrm.Read(buffer, 0, buffer.Length)
If readSize = 0 Then
Exit While
End If
fs.Write(buffer, 0, readSize)
End While
fs.Close()
resStrm.Close()
ftpRes.Close()
End Sub
Private Function OnRemoteCertificateValidationCallback(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
' 「SSL証明書の使用は問題なし」と示す
Return True
End Function
'WebUrlSry("https://www.yahoo.co.jp/")
Sub WebUrlSry(UrlPh)
Dim Path As String = UrlPh
Dim enc As Encoding = Encoding.GetEncoding("UTF-8")
Dim url As String = Path
'SSL通信対策
ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls Or Net.SecurityProtocolType.Tls11 Or Net.SecurityProtocolType.Tls12
Dim req As WebRequest = WebRequest.Create(url)
Dim res As WebResponse = req.GetResponse()
Dim st As Stream = res.GetResponseStream()
Dim sr As StreamReader = New StreamReader(st, enc)
Dim html As String = sr.ReadToEnd()
sr.Close()
st.Close()
End Sub
'Web上に表示された値が html に格納されるため、この値を活用したプログラミングも可能。
1) 「ツール」→「オプション」を開く
2) 「Windows フォーム デザイナー」にある「レイアウト設定」で
グリッドに合わせる:True
グリッドの表示:True
レイアウトモード:SnapToGrid
既定のグリッド セル サイズ:2,2
3) 「OK」で閉じる
サイズが変わって場合、対処方法は、プロパティ設定はデフォルトに戻し以下のように記述します。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right
End Sub
※インターネットの各サイトには、プロパティでやる方法をいくつか紹介されていますが、
経験上、解決に至りませんでした。
この方法であれば、問題は回避できますので、試してみてください。
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim FNm As String = "Book1.xlsx"
Dim SPath As String = "C:\Users\○○○○○\Desktop\"
Dim xl = CreateObject("Excel.Application")
xl.Visible = False
Dim xlwb = xl.workbooks.Open(SPath & FNm, Password:="test", WriteResPassword:="test")
Dim oSheet As Excel.Worksheet
oSheet = xlwb.Worksheets(1)
oSheet.Range("A1").Value = "今の日時"
oSheet.Range("A2").Value = "'" & Now().ToString("yyyy/MM/dd HH:mm:ss")
xl.Visible = True
SPath &= FNm
xlwb.Save '上書き保存
'xlwb.SaveAs(SPath, Password:="test2", WriteResPassword:="test2") '名前を付けて保存
xlwb.close
MessageBox.Show("完了")
End Sub
Function GetModuleFileNameEx(ByVal hProcess As IntPtr, ByVal hModule As IntPtr, ByVal lpBaseName As StringBuilder, ByVal nSize As Integer) As UInteger
End Function
Sub KillScreenSaver()
Dim enumerator As IEnumerator = Process.GetProcesses().GetEnumerator()
While enumerator.MoveNext()
Using p As Process = enumerator.Current
Try
Dim sb As New StringBuilder(1024)
GetModuleFileNameEx(p.Handle, IntPtr.Zero, sb, sb.Capacity)
If Path.GetExtension(sb.ToString()).ToUpper() = ".SCR" Then
p.Kill()
End If
Catch ex As Exception
End Try
End Using
End While
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call KillScreenSaver()
Me.Close()
End Sub
1) https://developer.microsoft.com/en-us/microsoft-edge/webview2/ から Evergreen Bootstrapper を Download します。
2) ダウンロードしたらWebView2の機能を使うすべてのPCにインストールします。
3) 開発ソフトでは、
「ツール」→「NuGet パッケージ マネージャー」→「パッケージ マネージャー設定」
で、パッケージの管理のプルダウンで「PackageReference」に切り替えて「OK」をクリックします。
4) 次も開発ソフトで、
「ツール」→「NuGet パッケージ マネージャー」→「ソリューションの NuGet パッケージの管理」
で、「参照」で「Microsoft.Web.WebView2」で検索し「安定板」をインストールします。
インストールが完了すると「ツールボックス」の領域に「WebView2」が表示されます。
Private Sub EnterTab_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
Me.SelectNextControl(sender, True, True, True, True)
e.Handled = True
End If
End Sub
'InstalledFontCollectionオブジェクトの取得
Dim InstalledFont As New System.Drawing.Text.InstalledFontCollection
'インストール済みフォントを取得してコンボボックスに表示する
Dim FontFamilies As FontFamily() = InstalledFont.Families
For Each f As FontFamily In FontFamilies
ComboBox1.Items.Add(f.Name)
Next f
'最背面に移動させる
Button1.SendToBack()
'最前面に移動させる
Button1.BringToFront()