webview2でファイルのダウンロード先やファイル名を変更する方法

あんまり古いWebView2だとDownloadStartingが無いので注意

Private Sub CoreWebView2_DownloadStarting(sender As Object, e As CoreWebView2DownloadStartingEventArgs)

    Dim downloadOperation : downloadOperation = e.DownloadOperation
    e.ResultFilePath = "C:\hogehoge\dl.zip"

End Sub

...

AddHandler FormE.view2.CoreWebView2.DownloadStarting, AddressOf CoreWebView2_DownloadStarting

ネタ元

www.fixes.pub

.net UNIX時間取得方法

・C#

using System;

class Program
{
  static void Main()
  {
    // 現在日時を表すDateTimeオブジェクトを取得
    DateTime targetTime = DateTime.Now;
   
    long unixTime = GetUnixTime(targetTime);
    Console.WriteLine(unixTime.ToString());
    // 出力例:1266864932

    Console.ReadLine();
  }

  // UNIXエポックを表すDateTimeオブジェクトを取得
  private static DateTime UNIX_EPOCH =
    new DateTime(1970, 1, 1, 0, 0, 0, 0);

  public static long GetUnixTime(DateTime targetTime)
  {
    // UTC時間に変換
    targetTime = targetTime.ToUniversalTime();

    // UNIXエポックからの経過時間を取得
    TimeSpan elapsedTime = targetTime - UNIX_EPOCH;
   
    // 経過秒数に変換
    return (long)elapsedTime.TotalSeconds;
  }
}

・VB.net

Module Module1

  Sub Main()

    ' 現在日時を表すDateTimeオブジェクトを取得
    Dim targetTime As DateTime = DateTime.Now
   
    Dim unixTime As Long = GetUnixTime(targetTime)
    Console.WriteLine(unixTime.ToString())
    ' 出力例:1266864932

    Console.ReadLine()

  End Sub

  ' UNIXエポックを表すDateTimeオブジェクトを取得
  Private UNIX_EPOCH As DateTime = _
    New DateTime(1970, 1, 1, 0, 0, 0, 0)

  Private Function GetUnixTime(ByVal targetTime As DateTime) As Long

    ' UTC時間に変換
    targetTime = targetTime.ToUniversalTime()

    ' UNIXエポックからの経過時間を取得
    Dim elapsedTime As TimeSpan = targetTime - UNIX_EPOCH

    ' 経過秒数に変換
    Return CType(elapsedTime.TotalSeconds, Long)

  End Function

End Module

VB.NETでファイルを削除する方法色々

ファイルが存在してなくてもエラーにならない方法

  • File.Delete メソッドを使用する方法
Dim sFilePath As String = "C:\work\myfile.txt"

System.IO.File.Delete(sFilePath)
Dim sFilePath As String = "C:\work\myfile.txt"
 
System.IO.File.Delete(sFilePath)

  • FileInfo クラスを使用する方法
Dim sFilePath As String = "C:\work\myfile.txt"

Dim finfo As New System.IO.FileInfo(sFilePath)
finfo.Delete()
Dim sFilePath As String = "C:\work\myfile.txt"
 
Dim finfo As New System.IO.FileInfo(sFilePath)
finfo.Delete()

ファイルが無いとエラーを発生する方法

  • FileSystem.DeleteFile メソッドを使用する方法
Dim sFilePath As String = "C:\work\myfile.txt"

My.Computer.FileSystem.DeleteFile(sFilePath)
Dim sFilePath As String = "C:\work\myfile.txt"
 
My.Computer.FileSystem.DeleteFile(sFilePath)

ネタ元

note.websmil.com

エクセルでセルの値を一括して1000で割る

・適当なセルに「1000」と入力します。
・[ホーム]タブ→[コピー]をクリックします。
・割りたいセルをドラッグして範囲選択します。
・[ホーム]タブ→[貼り付けの▼]→[形式を選択して貼り付け]をクリックします。
・[除算]をクリックし、[OK]をクリックします。
・割れました

ネタ元

incloop.com

Chromeによる C++でPOST送信するデータの簡単な取得方法

DevToolで

・「Network」をクリックします。
・「Record network log」の部分で、ここが赤くなっているかを確認します。黒のときはクリックすると赤になります。
・「Doc」をクリックします。「Name」の部分に現在アクセスしているページのファイルが表示されます。
・「Name」に表示されたファイル名をクリックします。右側にネットワークログを表示するボックスが表示されます。
・「Headers」「Preview」「Response」「Timing」という4つのタブが表示されますので「Headers」を選択します。デフォルトでは「Headers」が選択されているはずです。
「Headers」の中の一番下の「Form Data」の部分に、POSTで渡されたパラメータの内容が表示されます。
・ViewSorceをクリックすれば、エンコード済のコードになってくれるので便利

ネタ元

www.tsukimi.net

C# UIAutomationCondition でEdgeの操作

大雑把に言えばUIAutomationを使って、ほしい要素(エレメント)を探して取得して、操作する という感じ。

IUIAutomationConditionで探す要素を設定して
hogeElement.FindAll()でIUIAutomationConditionを指定して要素を探索。
見つかった要素が塊で取得できるのでループでバラしながら探す。
という流れ

・参照の追加でUIAutomationClientを追加
f:id:shikaku:20211005210155p:plain

・操作したい要素のConttoleTypeなど、情報を調べるには「Inspent」を使う
f:id:shikaku:20211005210351p:plain

...

// EdgeのアドレスバーのURL取得

int UIA_ControlTypePropertyId = 30003;
IUIAutomation uiAutomation = new CUIAutomation8();
IUIAutomationElement rootElement = uiAutomation.GetRootElement();
UIAutomationClient.IUIAutomationElement elmEdge;

string eURL = "";
{

    int UIA_WindowControlTypeId = 50032;

    IUIAutomationCondition condition =
        uiAutomation.CreatePropertyCondition(
            UIA_ControlTypePropertyId, UIA_WindowControlTypeId
        );

    UIAutomationClient.IUIAutomationElementArray aryWindowControls;
    aryWindowControls = rootElement.FindAll(TreeScope.TreeScope_Subtree, condition);
    elmEdge = null;
    for (int i = 0; i < aryWindowControls.Length; i++)
    {
        string name = aryWindowControls.GetElement(i).CurrentName;
        string classname = aryWindowControls.GetElement(i).CurrentClassName;
        name = name.ToLower();

        if (1 <= name.IndexOf("microsoft​ edge"))
        {

            elmEdge = aryWindowControls.GetElement(i);

            int UIA_EditControlTypeId = 50004;
            IUIAutomationCondition condition2 =
                uiAutomation.CreatePropertyCondition(
                    UIA_ControlTypePropertyId, UIA_EditControlTypeId
                );

            UIAutomationClient.IUIAutomationElementArray aryWindowControls2;
            aryWindowControls2 = elmEdge.FindAll(TreeScope.TreeScope_Subtree, condition2);

            for (int i2 = 0; i2 < aryWindowControls2.Length; i2++)
            {
                name = aryWindowControls2.GetElement(i2).CurrentName;
                if (name == "アドレスと検索バー")
                {
                    int UIA_ValueValuePropertyId = 30045;
                    eURL = aryWindowControls2.GetElement(i2).GetCurrentPropertyValue(UIA_ValueValuePropertyId);
                    break;
                }
            }

            break;
        }
    }
}

....


// Edgeの最後のタブを閉じる
int last_iptn = -1;
if (elmEdge != null)
{
    int UIA_ButtonControlTypeId = 50000;
    IUIAutomationCondition condition3 =
        uiAutomation.CreatePropertyCondition(
            UIA_ControlTypePropertyId, UIA_ButtonControlTypeId
        );

    UIAutomationClient.IUIAutomationElementArray aryWindowControls3;
    aryWindowControls3 = elmEdge.FindAll(TreeScope.TreeScope_Subtree, condition3);

    for (int i3 = 0; i3 < aryWindowControls3.Length; i3++)
    {
        string name = aryWindowControls3.GetElement(i3).CurrentName;
        if (name == "タブを閉じる")
        {
            last_iptn = i3;
        }
    }

    if (last_iptn != -1)
    {
        IUIAutomationInvokePattern iptn;
        int UIA_InvokePatternId = 10000;
        iptn = aryWindowControls3.GetElement(last_iptn).GetCurrentPattern(UIA_InvokePatternId);
        iptn.Invoke();
    }
}