質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

1回答

6609閲覧

Windows.Storageの参照の追加について(C# コンソールアプリケーション)

Wish_N

総合スコア35

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2020/07/12 11:03

現在、キャッシュ情報を利用したアプリケーションの作成を下記の環境で行っています。

開発環境:VisualStudio2019
フレームワーク:.NET Flamework4.7.2
プロジェクト種類:コンソールアプリケーション

StorageFolderクラス等を使用したいため、
using Windows.Storage; を追加したいのですが、参照を見ても、nugetを見ても見つかりません。

プロジェクトがコンソールアプリケーションの場合は、参照に追加することはできないのでしょうか。

必要な情報があれば追記いたします。

ご回答お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

Windows Runtime API使う場合は、Microsoft.Windows.SDK.ContractsをNuGetしてください。

何でもかんでも使えるというわけではないと思いますが、
ファイルとフォルダーの列挙と照会 - UWP applications | Microsoft Docs
のサンプルは実行できました。


あまり詳しくないので何が問題なのかわからないです^^;
同じようにコンソールアプリを.NET Flamework4.7.2で作ったのですが。

cs:Program.cs

1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.Threading.Tasks; 5using Windows.Storage; 6 7namespace Questions277082 8{ 9 class Program 10 { 11 static async Task Main() 12 { 13 StorageFolder picturesFolder = KnownFolders.PicturesLibrary; 14 StringBuilder outputText = new StringBuilder(); 15 16 IReadOnlyList<StorageFile> fileList = await picturesFolder.GetFilesAsync(); 17 18 outputText.AppendLine("Files:"); 19 foreach(StorageFile file in fileList) 20 { 21 outputText.Append(file.Name + "\n"); 22 } 23 24 IReadOnlyList<StorageFolder> folderList = await picturesFolder.GetFoldersAsync(); 25 26 outputText.AppendLine("Folders:"); 27 foreach(StorageFolder folder in folderList) 28 { 29 outputText.Append(folder.DisplayName + "\n"); 30 } 31 32 Console.WriteLine(outputText); 33 Console.ReadKey(); 34 } 35 } 36}

xml:Questions277082.csproj

1<?xml version="1.0" encoding="utf-8"?> 2<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3 <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" /> 4 <PropertyGroup> 5 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 6 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 7 <ProjectGuid>{C9C0ADED-9797-41F4-8BFC-A0A770D3AC5A}</ProjectGuid> 8 <OutputType>Exe</OutputType> 9 <RootNamespace>Questions277082</RootNamespace> 10 <AssemblyName>Questions277082</AssemblyName> 11 <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> 12 <FileAlignment>512</FileAlignment> 13 <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 14 <Deterministic>true</Deterministic> 15 </PropertyGroup> 16 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 17 <PlatformTarget>AnyCPU</PlatformTarget> 18 <DebugSymbols>true</DebugSymbols> 19 <DebugType>full</DebugType> 20 <Optimize>false</Optimize> 21 <OutputPath>bin\Debug\</OutputPath> 22 <DefineConstants>DEBUG;TRACE</DefineConstants> 23 <ErrorReport>prompt</ErrorReport> 24 <WarningLevel>4</WarningLevel> 25 </PropertyGroup> 26 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> 27 <PlatformTarget>AnyCPU</PlatformTarget> 28 <DebugType>pdbonly</DebugType> 29 <Optimize>true</Optimize> 30 <OutputPath>bin\Release\</OutputPath> 31 <DefineConstants>TRACE</DefineConstants> 32 <ErrorReport>prompt</ErrorReport> 33 <WarningLevel>4</WarningLevel> 34 </PropertyGroup> 35 <ItemGroup> 36 <Reference Include="System" /> 37 <Reference Include="System.Core" /> 38 <Reference Include="System.Xml.Linq" /> 39 <Reference Include="System.Data.DataSetExtensions" /> 40 <Reference Include="Microsoft.CSharp" /> 41 <Reference Include="System.Data" /> 42 <Reference Include="System.Net.Http" /> 43 <Reference Include="System.Xml" /> 44 </ItemGroup> 45 <ItemGroup> 46 <Compile Include="Program.cs" /> 47 <Compile Include="Properties\AssemblyInfo.cs" /> 48 </ItemGroup> 49 <ItemGroup> 50 <None Include="App.config" /> 51 </ItemGroup> 52 <ItemGroup> 53 <PackageReference Include="Microsoft.Windows.SDK.Contracts"> 54 <Version>10.0.19041.1</Version> 55 </PackageReference> 56 </ItemGroup> 57 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 58</Project>

.NET 6 以降はTFMの追加のみで、使用できるようになっています。
デスクトップ アプリで Windows ランタイム API を呼び出す - Windows apps | Microsoft Learn

投稿2020/07/12 12:16

編集2023/07/22 09:08
TN8001

総合スコア9321

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Wish_N

2020/07/12 12:54

Microsoft.Windows.SDK.ContractsをNuGetしました。 追加後にusingに追加するのは、using Windows.Storage; であっていますでしょうか? NuGet後でも赤線は消えず、StorageFolderクラス等は使用できない状況にあります。
Wish_N

2020/07/12 13:54

追記いただきありがとうございます。 追記いただいたソースをコピーしてプロジェクトを作ったのですが、 using Windows.Storage; は使えていないです。 Windows Runtime API使う場合は、Microsoft.Windows.SDK.ContractsをNuGetも行いました。 原因として考えられることを教えていただきたいです。 Web上でも同様の事象は発見できなかったため、ヒント等でも教えていただけると幸いです。
TN8001

2020/07/12 14:35 編集

[デスクトップ アプリで Windows ランタイム API を呼び出す | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/apps/desktop/modernize/desktop-to-uwp-enhance > NuGet オプションを使用するには これがMicrosoft.Windows.SDK.Contractsを入れるだけの新しい方法です。 Windows 10のバージョンによって入れるMicrosoft.Windows.SDK.Contractsのバージョンが変わるようです。 > 必要な参照を手動で追加するには こちらは古い方法ですが、前にやったときはかなり苦戦した記憶があります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問