2017-01-21 4 views
1

は、私がチェックする機能を作成したファイルにデータを追加します。UWP:作成し、確認し、ファイル「test.txtの」が存在する場合

Public Function CheckIfFileExists(sFileName As String) As Integer 

    Dim Location As Windows.Storage.StorageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation 
    Dim lstfiles = Location.GetFilesAsync(Search.CommonFileQuery.OrderByName) 
    Dim foundfiles = lstfiles.GetResults 
    Dim bFound As Integer 
    bFound = 0 

    If foundfiles IsNot Nothing Then 

     For Each sFile In foundfiles 
      If sFile.Name = sFileName Then 
       bFound = 1 
       Exit For 
      End If 
     Next 
    End If 
    Return bFound 

End Function 

を今、私が最初に私の主な機能にCheckIfFileExists(FILENAME)を呼び出すとき、例えば

CheckIfFileExists("test.txt") 

、それは私が予期しない時にメソッド呼び出しだと言って、「System.Runtime.InteropServices.COMException」を返します。

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll 

私は非同期関数を同期関数で使用しているという事実に関連していますか?

「はい」の場合、機能を非同期に変更するにはどうすればよいですか?

ありがとうございます!

Public Async Function CheckIfFileExists(sFileName As String) As Task(Of Integer) 

    Dim Location As Windows.Storage.StorageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation 
    Dim lstfiles = Await Location.GetFilesAsync(Search.CommonFileQuery.OrderByName) 
    Dim foundfiles = lstfiles 
    Dim bFound As Integer 
    bFound = 0 

    If foundfiles IsNot Nothing Then 
     For Each sFile In foundfiles 
      If sFile.Name = sFileName Then 
       bFound = 1 
       Exit For 
      End If 
     Next 
    End If 
    Return bFound 
End Function 

そしてCheckIfFileExists方法:

+0

を[MCVE]を提供してください。 – IInspectable

+0

要件を満たすために変更された質問 – bluefox

+0

これは[mcve]ではありません。 – IInspectable

答えて

0

Is it related to the fact that I use asynchronous function in a synchronous function?

はい、あなたは常に非同期GetFilesAsync方法待つ必要があります

Dim b As Boolean = Await CheckIfFileExists("test.txt") 
+0

上記の関数を使用すると、大いに愛されるエラーがスローされます。** .... CheckIfFileExists(System.String) 'は、' System.Threading.Tasks.Task 'という型のパラメータをその署名に含みます。このジェネリック型は有効なWindowsランタイム型ではありませんが、型または汎用パラメータは有効なWindowsランタイム型のインターフェイスを実装します。代わりに、メソッドのシグネチャのタイプ 'Task'を次のタイプのいずれかに変更することを検討してください。.... ** – bluefox

+0

Webでいくつかの調査を行い、非同期関数で提案した関数をラップしていることがわかりました、ここに記載: [link](https://marcominerva.wordpress.com/2013/03/21/how-to-expose-async-methods-in-a-windows-runtime-component/)。さらに、 'CheckIfFileExists'をプライベートとして置く必要があります!! – bluefox

関連する問題