回答編集履歴
4
見直しキャンペーン中
answer
CHANGED
@@ -5,8 +5,8 @@
|
|
5
5
|
`Properties`の中の`Resources.resx`を開いて、wavファイルをドラッグ&ドロップで入れてください。
|
6
6
|

|
7
7
|
|
8
|
-
Propertiesがない場合(.NET Coreや.NET 5以降だとデフォルトで作られません)
|
8
|
+
`Properties`がない場合(.NET Coreや.NET 5以降だとデフォルトで作られません)
|
9
|
-
Propertiesフォルダを作って中に「リソース ファイル」`Resources.resx`を新しい項目から追加します。
|
9
|
+
`Properties`フォルダを作って中に「リソース ファイル」`Resources.resx`を新しい項目から追加します。
|
10
10
|
|
11
11
|
↓のように使用します。
|
12
12
|
```cs
|
3
見直しキャンペーン中
answer
CHANGED
@@ -3,22 +3,24 @@
|
|
3
3
|
まず結論から(画像なんかはまた話が違ってきます)
|
4
4
|
## exeに埋め込みたい場合:
|
5
5
|
`Properties`の中の`Resources.resx`を開いて、wavファイルをドラッグ&ドロップで入れてください。
|
6
|
-
```cs
|
7
|
-
new SoundPlayer(Properties.Resources.PropertiesResources).Play();
|
8
|
-
```
|
9
|
-
のように使用します。
|
10
6
|

|
11
7
|
|
12
8
|
Propertiesがない場合(.NET Coreや.NET 5以降だとデフォルトで作られません)
|
13
9
|
Propertiesフォルダを作って中に「リソース ファイル」`Resources.resx`を新しい項目から追加します。
|
14
10
|
|
11
|
+
↓のように使用します。
|
12
|
+
```cs
|
13
|
+
new SoundPlayer(Properties.Resources.PropertiesResources).Play();
|
14
|
+
```
|
15
|
+
|
15
16
|
## 相対パスで指定したい場合:
|
16
17
|
wavファイルのプロパティでビルドアクションをなしかコンテンツにして、「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」としてください。
|
18
|
+

|
19
|
+
|
20
|
+
(カレントディレクトリを変更しなければ)↓のように使用します。
|
17
21
|
```cs
|
18
22
|
new SoundPlayer("Resources/None.wav").Play();
|
19
23
|
```
|
20
|
-
(カレントディレクトリを変更しなければ)のように使用します。
|
21
|
-

|
22
24
|
|
23
25
|
---
|
24
26
|
|
2
見直しキャンペーン中
answer
CHANGED
@@ -1,192 +1,192 @@
|
|
1
|
-
> ローカルパス(リソース?)から指定する方法がわかりません。
|
2
|
-
|
3
|
-
まず結論から(画像なんかはまた話が違ってきます)
|
4
|
-
## exeに埋め込みたい場合:
|
5
|
-
`Properties`の中の`Resources.resx`を開いて、wavファイルをドラッグ&ドロップで入れてください。
|
6
|
-
```
|
7
|
-
new SoundPlayer(Properties.Resources.PropertiesResources).Play();
|
8
|
-
```
|
9
|
-
のように使用します。
|
10
|
-

|
11
|
-
|
12
|
-
Propertiesがない場合(.NET Coreや.NET 5以降だとデフォルトで作られません)
|
13
|
-
Propertiesフォルダを作って中に「リソース ファイル」`Resources.resx`を新しい項目から追加します。
|
14
|
-
|
15
|
-
## 相対パスで指定したい場合:
|
16
|
-
wavファイルのプロパティでビルドアクションをなしかコンテンツにして、「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」としてください。
|
17
|
-
```
|
18
|
-
new SoundPlayer("Resources/None.wav").Play();
|
19
|
-
```
|
20
|
-
(カレントディレクトリを変更しなければ)のように使用します。
|
21
|
-

|
22
|
-
|
23
|
-
---
|
24
|
-
|
25
|
-
わたしもこのあたり結構難しいなと感じていたのでまとめてみました。
|
26
|
-
|
27
|
-
### ビルドアクション - リソース(Resource)
|
28
|
-
WPFで新たに入った埋め込み方法。
|
29
|
-
埋め込みリソースと仕組みは同じだが、xamlから簡単に読めるようになっている。
|
30
|
-
画像なんかはこれ一択でしょう。
|
31
|
-
|
32
|
-
### ビルドアクション - コンテンツ(Content)
|
33
|
-
埋め込まれません。
|
34
|
-
「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」にしないと意味がない。
|
35
|
-
カレントディレクトリを変更していなければ、相対パスで読める。
|
36
|
-
なし(None)との違いはよくわからない^^;
|
37
|
-
|
38
|
-
### ビルドアクション - なし(None)
|
39
|
-
埋め込まれません。
|
40
|
-
「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」にしないと意味がない。
|
41
|
-
カレントディレクトリを変更していなければ、相対パスで読める。
|
42
|
-
|
43
|
-
### ビルドアクション - 埋め込みリソース(Embedded Resource)
|
44
|
-
昔からある埋め込み方法。
|
45
|
-
ビルドアクションでわざわざこれを選ぶ理由はない?
|
46
|
-
|
47
|
-
### Properties.Resources.resx
|
48
|
-
Windows フォームでリソースといえばこれ。
|
49
|
-
埋め込みリソースになるが専用のGUIがついていてわかりやすいし、アクセスが簡単になるように自動でコード生成される。
|
50
|
-
xamlで使うつもりがないのであれば、これでいいのかな?
|
51
|
-
|
52
|
-
---
|
53
|
-
|
54
|
-
[アプリケーションのリソース ファイル、コンテンツ ファイル、およびデータ ファイル - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/app-development/wpf-application-resource-content-and-data-files?view=netframeworkdesktop-4.8)
|
55
|
-
|
56
|
-
[One more time about Resource vs. EmbeddedResource – Ivan Krivyakov](https://ikriv.com/blog/?p=1530)
|
57
|
-
|
58
|
-
|
59
|
-
```
|
60
|
-
<Window
|
61
|
-
x:Class="Questions375405.MainWindow"
|
62
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
63
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
64
|
-
Width="800"
|
65
|
-
Height="450">
|
66
|
-
<StackPanel>
|
67
|
-
<Button Click="ResourceButton_Click" Content="Resource" />
|
68
|
-
<Button Click="ContentButton_Click" Content="Content" />
|
69
|
-
<Button Click="NoneButton_Click" Content="None" />
|
70
|
-
<Button Click="EmbeddedResourceButton_Click" Content="EmbeddedResource" />
|
71
|
-
<Button Click="PropertiesResourcesButton_Click" Content="PropertiesResources" />
|
72
|
-
</StackPanel>
|
73
|
-
</Window>
|
74
|
-
```
|
75
|
-
|
76
|
-
```
|
77
|
-
using System;
|
78
|
-
using System.Media;
|
79
|
-
using System.Reflection;
|
80
|
-
using System.Windows;
|
81
|
-
|
82
|
-
namespace Questions375405
|
83
|
-
{
|
84
|
-
public partial class MainWindow : Window
|
85
|
-
{
|
86
|
-
public MainWindow()
|
87
|
-
{
|
88
|
-
InitializeComponent();
|
89
|
-
|
90
|
-
//Environment.CurrentDirectory = @"C:\";
|
91
|
-
}
|
92
|
-
|
93
|
-
// ビルドアクション - リソース(Resource)
|
94
|
-
private void ResourceButton_Click(object sender, RoutedEventArgs e)
|
95
|
-
{
|
96
|
-
var uri = new Uri("Resources/Resource.wav", UriKind.Relative);
|
97
|
-
var info = Application.GetResourceStream(uri);
|
98
|
-
new SoundPlayer(info.Stream).Play();
|
99
|
-
}
|
100
|
-
|
101
|
-
// ビルドアクション - コンテンツ(Content)
|
102
|
-
private void ContentButton_Click(object sender, RoutedEventArgs e)
|
103
|
-
{
|
104
|
-
var uri = new Uri("Resources/Content.wav", UriKind.Relative);
|
105
|
-
var info = Application.GetContentStream(uri);
|
106
|
-
new SoundPlayer(info.Stream).Play();
|
107
|
-
|
108
|
-
// カレントディレクトリを変更しないのであればこれでいい
|
109
|
-
//new SoundPlayer("Resources/Content.wav").Play();
|
110
|
-
}
|
111
|
-
|
112
|
-
// ビルドアクション - なし(None)
|
113
|
-
private void NoneButton_Click(object sender, RoutedEventArgs e)
|
114
|
-
{
|
115
|
-
var uri = new Uri("Resources/None.wav", UriKind.Relative);
|
116
|
-
var info = Application.GetRemoteStream(uri);
|
117
|
-
new SoundPlayer(info.Stream).Play();
|
118
|
-
|
119
|
-
// カレントディレクトリを変更しないのであればこれでいい
|
120
|
-
//new SoundPlayer("Resources/None.wav").Play();
|
121
|
-
}
|
122
|
-
|
123
|
-
// ビルドアクション - 埋め込みリソース
|
124
|
-
private void EmbeddedResourceButton_Click(object sender, RoutedEventArgs e)
|
125
|
-
{
|
126
|
-
var assembly = Assembly.GetExecutingAssembly();
|
127
|
-
var stream = assembly.GetManifestResourceStream("Questions375405.Resources.EmbeddedResource.wav");
|
128
|
-
new SoundPlayer(stream).Play();
|
129
|
-
}
|
130
|
-
|
131
|
-
// Properties.Resources.resx
|
132
|
-
private void PropertiesResourcesButton_Click(object sender, RoutedEventArgs e)
|
133
|
-
{
|
134
|
-
new SoundPlayer(Properties.Resources.PropertiesResources).Play();
|
135
|
-
}
|
136
|
-
}
|
137
|
-
}
|
138
|
-
```
|
139
|
-
|
140
|
-
```xml
|
141
|
-
<Project Sdk="Microsoft.NET.Sdk">
|
142
|
-
|
143
|
-
<PropertyGroup>
|
144
|
-
<OutputType>WinExe</OutputType>
|
145
|
-
<TargetFramework>net6.0-windows</TargetFramework>
|
146
|
-
<Nullable>enable</Nullable>
|
147
|
-
<UseWPF>true</UseWPF>
|
148
|
-
</PropertyGroup>
|
149
|
-
|
150
|
-
<ItemGroup>
|
151
|
-
<None Remove="Resources\Content.wav" />
|
152
|
-
<None Remove="Resources\EmbeddedResource.wav" />
|
153
|
-
<None Remove="Resources\Resource.wav" />
|
154
|
-
</ItemGroup>
|
155
|
-
|
156
|
-
<ItemGroup>
|
157
|
-
<Content Include="Resources\Content.wav">
|
158
|
-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
159
|
-
</Content>
|
160
|
-
</ItemGroup>
|
161
|
-
|
162
|
-
<ItemGroup>
|
163
|
-
<EmbeddedResource Include="Resources\EmbeddedResource.wav" />
|
164
|
-
</ItemGroup>
|
165
|
-
|
166
|
-
<ItemGroup>
|
167
|
-
<Resource Include="Resources\Resource.wav" />
|
168
|
-
</ItemGroup>
|
169
|
-
|
170
|
-
<ItemGroup>
|
171
|
-
<Compile Update="Properties\Resources.Designer.cs">
|
172
|
-
<DesignTime>True</DesignTime>
|
173
|
-
<AutoGen>True</AutoGen>
|
174
|
-
<DependentUpon>Resources.resx</DependentUpon>
|
175
|
-
</Compile>
|
176
|
-
</ItemGroup>
|
177
|
-
|
178
|
-
<ItemGroup>
|
179
|
-
<EmbeddedResource Update="Properties\Resources.resx">
|
180
|
-
<Generator>ResXFileCodeGenerator</Generator>
|
181
|
-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
182
|
-
</EmbeddedResource>
|
183
|
-
</ItemGroup>
|
184
|
-
|
185
|
-
<ItemGroup>
|
186
|
-
<None Update="Resources\None.wav">
|
187
|
-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
188
|
-
</None>
|
189
|
-
</ItemGroup>
|
190
|
-
|
191
|
-
</Project>
|
1
|
+
> ローカルパス(リソース?)から指定する方法がわかりません。
|
2
|
+
|
3
|
+
まず結論から(画像なんかはまた話が違ってきます)
|
4
|
+
## exeに埋め込みたい場合:
|
5
|
+
`Properties`の中の`Resources.resx`を開いて、wavファイルをドラッグ&ドロップで入れてください。
|
6
|
+
```cs
|
7
|
+
new SoundPlayer(Properties.Resources.PropertiesResources).Play();
|
8
|
+
```
|
9
|
+
のように使用します。
|
10
|
+

|
11
|
+
|
12
|
+
Propertiesがない場合(.NET Coreや.NET 5以降だとデフォルトで作られません)
|
13
|
+
Propertiesフォルダを作って中に「リソース ファイル」`Resources.resx`を新しい項目から追加します。
|
14
|
+
|
15
|
+
## 相対パスで指定したい場合:
|
16
|
+
wavファイルのプロパティでビルドアクションをなしかコンテンツにして、「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」としてください。
|
17
|
+
```cs
|
18
|
+
new SoundPlayer("Resources/None.wav").Play();
|
19
|
+
```
|
20
|
+
(カレントディレクトリを変更しなければ)のように使用します。
|
21
|
+

|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
わたしもこのあたり結構難しいなと感じていたのでまとめてみました。
|
26
|
+
|
27
|
+
### ビルドアクション - リソース(Resource)
|
28
|
+
WPFで新たに入った埋め込み方法。
|
29
|
+
埋め込みリソースと仕組みは同じだが、xamlから簡単に読めるようになっている。
|
30
|
+
画像なんかはこれ一択でしょう。
|
31
|
+
|
32
|
+
### ビルドアクション - コンテンツ(Content)
|
33
|
+
埋め込まれません。
|
34
|
+
「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」にしないと意味がない。
|
35
|
+
カレントディレクトリを変更していなければ、相対パスで読める。
|
36
|
+
なし(None)との違いはよくわからない^^;
|
37
|
+
|
38
|
+
### ビルドアクション - なし(None)
|
39
|
+
埋め込まれません。
|
40
|
+
「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」にしないと意味がない。
|
41
|
+
カレントディレクトリを変更していなければ、相対パスで読める。
|
42
|
+
|
43
|
+
### ビルドアクション - 埋め込みリソース(Embedded Resource)
|
44
|
+
昔からある埋め込み方法。
|
45
|
+
ビルドアクションでわざわざこれを選ぶ理由はない?
|
46
|
+
|
47
|
+
### Properties.Resources.resx
|
48
|
+
Windows フォームでリソースといえばこれ。
|
49
|
+
埋め込みリソースになるが専用のGUIがついていてわかりやすいし、アクセスが簡単になるように自動でコード生成される。
|
50
|
+
xamlで使うつもりがないのであれば、これでいいのかな?
|
51
|
+
|
52
|
+
---
|
53
|
+
|
54
|
+
[アプリケーションのリソース ファイル、コンテンツ ファイル、およびデータ ファイル - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/app-development/wpf-application-resource-content-and-data-files?view=netframeworkdesktop-4.8)
|
55
|
+
|
56
|
+
[One more time about Resource vs. EmbeddedResource – Ivan Krivyakov](https://ikriv.com/blog/?p=1530)
|
57
|
+
|
58
|
+
|
59
|
+
```xml
|
60
|
+
<Window
|
61
|
+
x:Class="Questions375405.MainWindow"
|
62
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
63
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
64
|
+
Width="800"
|
65
|
+
Height="450">
|
66
|
+
<StackPanel>
|
67
|
+
<Button Click="ResourceButton_Click" Content="Resource" />
|
68
|
+
<Button Click="ContentButton_Click" Content="Content" />
|
69
|
+
<Button Click="NoneButton_Click" Content="None" />
|
70
|
+
<Button Click="EmbeddedResourceButton_Click" Content="EmbeddedResource" />
|
71
|
+
<Button Click="PropertiesResourcesButton_Click" Content="PropertiesResources" />
|
72
|
+
</StackPanel>
|
73
|
+
</Window>
|
74
|
+
```
|
75
|
+
|
76
|
+
```cs
|
77
|
+
using System;
|
78
|
+
using System.Media;
|
79
|
+
using System.Reflection;
|
80
|
+
using System.Windows;
|
81
|
+
|
82
|
+
namespace Questions375405
|
83
|
+
{
|
84
|
+
public partial class MainWindow : Window
|
85
|
+
{
|
86
|
+
public MainWindow()
|
87
|
+
{
|
88
|
+
InitializeComponent();
|
89
|
+
|
90
|
+
//Environment.CurrentDirectory = @"C:\";
|
91
|
+
}
|
92
|
+
|
93
|
+
// ビルドアクション - リソース(Resource)
|
94
|
+
private void ResourceButton_Click(object sender, RoutedEventArgs e)
|
95
|
+
{
|
96
|
+
var uri = new Uri("Resources/Resource.wav", UriKind.Relative);
|
97
|
+
var info = Application.GetResourceStream(uri);
|
98
|
+
new SoundPlayer(info.Stream).Play();
|
99
|
+
}
|
100
|
+
|
101
|
+
// ビルドアクション - コンテンツ(Content)
|
102
|
+
private void ContentButton_Click(object sender, RoutedEventArgs e)
|
103
|
+
{
|
104
|
+
var uri = new Uri("Resources/Content.wav", UriKind.Relative);
|
105
|
+
var info = Application.GetContentStream(uri);
|
106
|
+
new SoundPlayer(info.Stream).Play();
|
107
|
+
|
108
|
+
// カレントディレクトリを変更しないのであればこれでいい
|
109
|
+
//new SoundPlayer("Resources/Content.wav").Play();
|
110
|
+
}
|
111
|
+
|
112
|
+
// ビルドアクション - なし(None)
|
113
|
+
private void NoneButton_Click(object sender, RoutedEventArgs e)
|
114
|
+
{
|
115
|
+
var uri = new Uri("Resources/None.wav", UriKind.Relative);
|
116
|
+
var info = Application.GetRemoteStream(uri);
|
117
|
+
new SoundPlayer(info.Stream).Play();
|
118
|
+
|
119
|
+
// カレントディレクトリを変更しないのであればこれでいい
|
120
|
+
//new SoundPlayer("Resources/None.wav").Play();
|
121
|
+
}
|
122
|
+
|
123
|
+
// ビルドアクション - 埋め込みリソース
|
124
|
+
private void EmbeddedResourceButton_Click(object sender, RoutedEventArgs e)
|
125
|
+
{
|
126
|
+
var assembly = Assembly.GetExecutingAssembly();
|
127
|
+
var stream = assembly.GetManifestResourceStream("Questions375405.Resources.EmbeddedResource.wav");
|
128
|
+
new SoundPlayer(stream).Play();
|
129
|
+
}
|
130
|
+
|
131
|
+
// Properties.Resources.resx
|
132
|
+
private void PropertiesResourcesButton_Click(object sender, RoutedEventArgs e)
|
133
|
+
{
|
134
|
+
new SoundPlayer(Properties.Resources.PropertiesResources).Play();
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
```
|
139
|
+
|
140
|
+
```xml
|
141
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
142
|
+
|
143
|
+
<PropertyGroup>
|
144
|
+
<OutputType>WinExe</OutputType>
|
145
|
+
<TargetFramework>net6.0-windows</TargetFramework>
|
146
|
+
<Nullable>enable</Nullable>
|
147
|
+
<UseWPF>true</UseWPF>
|
148
|
+
</PropertyGroup>
|
149
|
+
|
150
|
+
<ItemGroup>
|
151
|
+
<None Remove="Resources\Content.wav" />
|
152
|
+
<None Remove="Resources\EmbeddedResource.wav" />
|
153
|
+
<None Remove="Resources\Resource.wav" />
|
154
|
+
</ItemGroup>
|
155
|
+
|
156
|
+
<ItemGroup>
|
157
|
+
<Content Include="Resources\Content.wav">
|
158
|
+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
159
|
+
</Content>
|
160
|
+
</ItemGroup>
|
161
|
+
|
162
|
+
<ItemGroup>
|
163
|
+
<EmbeddedResource Include="Resources\EmbeddedResource.wav" />
|
164
|
+
</ItemGroup>
|
165
|
+
|
166
|
+
<ItemGroup>
|
167
|
+
<Resource Include="Resources\Resource.wav" />
|
168
|
+
</ItemGroup>
|
169
|
+
|
170
|
+
<ItemGroup>
|
171
|
+
<Compile Update="Properties\Resources.Designer.cs">
|
172
|
+
<DesignTime>True</DesignTime>
|
173
|
+
<AutoGen>True</AutoGen>
|
174
|
+
<DependentUpon>Resources.resx</DependentUpon>
|
175
|
+
</Compile>
|
176
|
+
</ItemGroup>
|
177
|
+
|
178
|
+
<ItemGroup>
|
179
|
+
<EmbeddedResource Update="Properties\Resources.resx">
|
180
|
+
<Generator>ResXFileCodeGenerator</Generator>
|
181
|
+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
182
|
+
</EmbeddedResource>
|
183
|
+
</ItemGroup>
|
184
|
+
|
185
|
+
<ItemGroup>
|
186
|
+
<None Update="Resources\None.wav">
|
187
|
+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
188
|
+
</None>
|
189
|
+
</ItemGroup>
|
190
|
+
|
191
|
+
</Project>
|
192
192
|
```
|
1
修正
answer
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
```
|
18
18
|
new SoundPlayer("Resources/None.wav").Play();
|
19
19
|
```
|
20
|
-
のように使用します。
|
20
|
+
(カレントディレクトリを変更しなければ)のように使用します。
|
21
21
|

|
22
22
|
|
23
23
|
---
|
@@ -40,13 +40,13 @@
|
|
40
40
|
「出力ディレクトリにコピー」を、「常にコピーする」または「新しい場合にコピーする」にしないと意味がない。
|
41
41
|
カレントディレクトリを変更していなければ、相対パスで読める。
|
42
42
|
|
43
|
-
### ビルドアクション - 埋め込みリソース
|
43
|
+
### ビルドアクション - 埋め込みリソース(Embedded Resource)
|
44
44
|
昔からある埋め込み方法。
|
45
45
|
ビルドアクションでわざわざこれを選ぶ理由はない?
|
46
46
|
|
47
47
|
### Properties.Resources.resx
|
48
48
|
Windows フォームでリソースといえばこれ。
|
49
|
-
GUIがついていてわかりやすいし、アクセス
|
49
|
+
埋め込みリソースになるが専用のGUIがついていてわかりやすいし、アクセスが簡単になるように自動でコード生成される。
|
50
50
|
xamlで使うつもりがないのであれば、これでいいのかな?
|
51
51
|
|
52
52
|
---
|