Program.csのMain()の中に、ログ出力前に以下を記載すればReleaseビルドのときにも出力されるようになる。
log4net.Config.XmlConfigurator.Configure();
AnyCPUでビルドして64bitで動作するEXEを32bitで動作するようにするには、以下のコマンドを実行する。
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\CorFlags.exe" 変更したいEXE.exe /32BITREQ+ /Force
実行すると以下のようにコンソールに出力される(変更が完了したかについては表示されないようだ)。
Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.8.3928.0
Copyright (c) Microsoft Corporation. All rights reserved.
変換完了後、実際に64bit環境で起動させてみて、タスクマネージャーの詳細タブから、該当のEXEのプロットフォームが「32ビット」となっていれば変更は完了している。
SQLite+EntityFrameworkの環境にて発生。
System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions. ---> System.Data.Entity.Core.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
場所 System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source)
場所 System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
場所 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
場所 System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
場所 System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
場所 System.Data.Entity.Internal.InternalContext.SaveChanges()
あるフォームのイベント発生時にレコード更新する処理を入れていたが、先に閉じたフォームのイベントが残っていて、同時に複数のフォームから同じレコードを更新していたことが原因だった。
今回は、閉じたフォームのイベントが確実に消えるようにしたところ解消された。
WindowsのAPIであるGetShortPathName関数を利用する。
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
以下のように呼び出す。
// ※ポイント!
// 長いパスの先頭に「\\?\」をつけないと、短いパスを取得できない
var longPath = @"\\?\C:\Users\a\Desktop\あいうえお\テキストファイル\111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.txt";
var shortPath = new StringBuilder(256);
if (GetShortPathName(longPath, shortPath, shortPath.Capacity) == 0)
{
// 取得失敗
}
else
{
// 取得成功。取得例 \\?\C:\Users\a\Desktop\あいう~1\テキス~1\111111~1.TXT
Console.WriteLine(shortPath);
}
Microsoft.Web.WebView2.WinForms.WebView2の以下の赤枠部分を非表示にする。
次のコードのように、非表示にしたいアイコンを複数指定可能。
webView.CoreWebView2.Settings.HiddenPdfToolbarItems =
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.ZoomOut /*縮小*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.ZoomIn /*拡大*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.FitPage /*幅に合わせる*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.Rotate /*回転*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.PageLayout /*ページ表示*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.Search /*検索*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.Save /*上書き保存*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.FullScreen /*PDF全画面表示を入力する*/ |
Microsoft.Web.WebView2.Core.CoreWebView2PdfToolbarItems.MoreSettings/*設定など*/;
try-convertというツールを利用して、以下の手順でコンバートする(Visual Studio 2022で作成した.NET Frramework4.8のWindowsフォームの前提)。
※実行する前に全ファイルを別フォルダなどにコピーしてバックアップしておくこと!
コンバートは自動でバックアップしてくれず、そのままプロジェクトが書き換えられるので注意!
これで、とりあえず.NET6としてコンバートが完了した。
以下の旧プロジェクトファイルが残るので、これらは削除してOK
「XXX.csproj.old(C#の場合)」「XXX.vbproj.old(VB.NETの場合)」
以下に、コンバート後に手修正した問題点を随時追記していく。
「警告 CA1416 この呼び出しサイトはすべてのプラットフォームで到達可能です。’Font’ は ‘windows’ でのみサポートされています。」というような「CA1416」の警告が出た場合は、とりあえず以下のように警告をつぶすことができる。
C#
#pragma warning disable CA1416
LabelText.Font = new System.Drawing.Font("Meiryo UI", 21.75F);
#pragma warning restore CA1416
VB.NET
#Disable Warning CA1416
LabelText.Font = New System.Drawing.Font("Meiryo UI", 21.75F)
#Enable Warning CA1416
上記のように#から始まるディレクティブを使って、警告がでないようにする。
フォーム自体のフォントが「Yu Gothic UI, 9pt」になるので、これを「MS UI Gothic, 9pt」に変更すれば元のデザインのままになった。
.NET Framework4.8のときは、以下を実行すると既定のブラウザで指定したURLのページが表示されていた
System.Diagnostics.Process.Start("https://juraku-software.net");
しかし、.NET6だとこれを実行すると「System.ComponentModel.Win32Exception: ‘An error occurred trying to start process ‘https://juraku-software.net’ with working directory ‘C:\~\bin\Debug\net6.0-windows’. 指定されたファイルが見つかりません。’」というエラーになってしまう。
そのため、.NET 6では以下のように修正する。
C#
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "https://juraku-software.net"
};
System.Diagnostics.Process.Start(processStartInfo);
VB.NET
Dim processStartInfo As New ProcessStartInfo With {
.UseShellExecute = True,
.FileName = "https://juraku-software.net"
}
System.Diagnostics.Process.Start(processStartInfo)
こういう実行時エラーが一番怖い・・・挙動を変えないで欲しかった・・・
フォーム自身(this)のControlsの中から、OfTypeを利用してLabelのみを抜き出せばOK
foreach (Label label in this.Controls.OfType<Label>())
{
label.ForeColor = Color.Red;
}
また、例えば対象のラベル名がlabel1~label5である場合、以下のように名前でFindすることもできる。
for (int i = 1; i <= 5; i++)
{
Control label = this.Controls.Find("label" + i, true).FirstOrDefault();
if (label is Label)
{
label.ForeColor = Color.Red;
}
}