Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Android上使用Xamarin.Forms创建文件?_C#_Android_Xamarin.forms_System.io.file - Fatal编程技术网

C# 如何在Android上使用Xamarin.Forms创建文件?

C# 如何在Android上使用Xamarin.Forms创建文件?,c#,android,xamarin.forms,system.io.file,C#,Android,Xamarin.forms,System.io.file,我想在我的android手机上创建一个公共文件,无需root访问即可找到。用户可以找到并阅读它的地方 我的当前代码在Visual Studio错误列表中没有给出错误。只有36条关于无法读取带有调试符号的mono.android.dll之类的文件以及关于未在我的FileSaver类中使用声明的异常的警告。目前,我认为这没有关系。我没有例外。我的代码已成功发送到文件创建警报 如果有人能把我推向正确的方向,或者知道我正在犯的错误,我会洗耳恭听 我的代码: XAML按钮部件 XAML.CS构造函数并

我想在我的android手机上创建一个公共文件,无需root访问即可找到。用户可以找到并阅读它的地方

我的当前代码在Visual Studio错误列表中没有给出错误。只有36条关于无法读取带有调试符号的mono.android.dll之类的文件以及关于未在我的FileSaver类中使用声明的异常的警告。目前,我认为这没有关系。我没有例外。我的代码已成功发送到文件创建警报

如果有人能把我推向正确的方向,或者知道我正在犯的错误,我会洗耳恭听

我的代码: XAML按钮部件


XAML.CS构造函数并单击部件

公共MyPage()
{
初始化组件();
CreateFile.Clicked+=OnCreateFileClicked;
}
私有异步void OnCreateFileClicked(对象发送方,EventArgs e)
{
IFileSaver FileSaver=DependencyService.Get();
if(等待FileSaver.CreateFile())
{
等待此.DisplayAlert(“文件已创建”、“已创建文件”、“确定”、“确定”);
}否则
{
等待此消息。DisplayAlert(“文件未创建”,“文件未创建”,“文件:(“,;-”);
}
}
接口

使用System.Threading.Tasks;
名称空间PvApp
{
/*
*您不能在PCL(公共类库)中使用C#friends文件、目录、Streamwriter等(system.io)。
*因此,我们在特定于平台的库中执行此操作。
*/
公共接口IFileSaver
{        
任务创建文件();
}
}
文件保存类

使用System.Threading.Tasks;
使用Xamarin.Forms;
使用PvApp.Droid.Assets;
[程序集:依赖项(typeof(FileSaver))]
命名空间PvApp.Droid.Assets
{ 
类文件保存器:IFileSaver
{
公共异步任务CreateFile()
{           
//我的文件,
string folder=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
stringpath=System.IO.path.Combine(文件夹“test.txt”);
尝试
{
System.IO.File.Create(路径);
返回等待任务。FromResult(true);
} 
捕获(System.Exception异常)
{
返回等待任务。FromResult(false);
}  
}
}
}
我做的事 1:

{System.UnauthorizedAccessException: Access to the path "/test.txt" is denied.
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001b7] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.File.Create (System.String path) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at PvApp.Droid.Assets.FileSaver+<CreateFile>d__0.MoveNext () [0x00058] in (Filepath to my FileSaver.cs and to function System.IO.File.Create(path);)
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
我在Xamarin大学看了这段视频,视频显示我可以使用System.IO名称空间中的一些老朋友,比如文件、目录等((4:14))

视频还告诉我,它们只能在特定于平台的项目中访问((4:35))

因此,我在PCL库中创建了一个接口,并在我的Android平台特定项目资产文件夹中添加了一个实现该接口的类。我在单击事件处理程序的页面中调用了该接口。代码运行正常,我收到了成功警报,但在我的设备上找不到该文件

2:

{System.UnauthorizedAccessException: Access to the path "/test.txt" is denied.
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001b7] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.File.Create (System.String path) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at PvApp.Droid.Assets.FileSaver+<CreateFile>d__0.MoveNext () [0x00058] in (Filepath to my FileSaver.cs and to function System.IO.File.Create(path);)
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
像这样的问题并不能解决我的问题:

3:

{System.UnauthorizedAccessException: Access to the path "/test.txt" is denied.
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001b7] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.File.Create (System.String path) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at PvApp.Droid.Assets.FileSaver+<CreateFile>d__0.MoveNext () [0x00058] in (Filepath to my FileSaver.cs and to function System.IO.File.Create(path);)
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
我检查了是否需要权限。内部存储不需要权限

4:

{System.UnauthorizedAccessException: Access to the path "/test.txt" is denied.
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001b7] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.File.Create (System.String path) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at PvApp.Droid.Assets.FileSaver+<CreateFile>d__0.MoveNext () [0x00058] in (Filepath to my FileSaver.cs and to function System.IO.File.Create(path);)
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
例如,我尝试了网站上的其他代码或StackOverflow问题,这些代码应该创建一个文件。Application.Context.FilesDir.Path,但没有任何帮助。我无法使其正常工作。为了这篇文章的篇幅,我将它们排除在外

已尝试的Riciprinis代码:

{System.UnauthorizedAccessException: Access to the path "/test.txt" is denied.
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001b7] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at System.IO.File.Create (System.String path) [0x00000] in <4d6eb5dfe2ab4eee884ef920069afd5f>:0 
 at PvApp.Droid.Assets.FileSaver+<CreateFile>d__0.MoveNext () [0x00058] in (Filepath to my FileSaver.cs and to function System.IO.File.Create(path);)
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
解决方案:

string folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;            
如果要添加请求运行时权限,请遵循以下解决方案:

string folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;            


如果要添加请求运行时权限,请执行此操作

是否确实没有收到异常?您的try/catch块似乎忽略了任何异常。它如何忽略任何异常?请帮助我理解它。它返回false,但您没有以任何方式记录异常,因此您不知道实际的底层异常是什么错误是。了解引发的特定异常将有助于调试我知道如果try块失败,CreateFile将返回false,但它不会失败,因此CreateFile将返回true。如果它返回true并且现在正在引发异常,则它可能正在创建您的文件。是否确实不是?如果您执行了file.Exists()文件在吗?你确定没有收到异常吗?你的try/catch块似乎忽略了任何异常。它是如何忽略任何异常的?请帮助我理解它。它返回false,但你没有以任何方式记录异常,因此你不知道实际的基本错误。知道具体的异常是什么b抛出将帮助您调试我知道如果try块失败,CreateFile将返回false,但它不会失败,因此CreateFile将返回true。如果它返回true,并且现在正在抛出异常,则它可能正在创建您的文件。您确定它不是吗?如果执行文件。Exists()文件在那里吗?我尝试了你的代码,但没有成功,除非我得到的异常指出了我的错误所在。请参阅我更新的问题帖子。你是否添加了内部存储权限?{System.UnauthorizedAccessException:Access to the path”/test.txt被拒绝。你确定吗?我尝试使用读写外部存储权限,但据我所知,没有内部存储权限。对于外部存储权限,请尝试跟随此视频尝试在此行中设置断点
string folder=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
并查看路径是否包含此路径
/
尝试更改
字符串文件夹=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string folder=Android.OS.Environment.ExternalStorageDirectory.AbsolutePath
并检查文件是否在本地存储中创建,如果没有创建,则问题是权限我尝试了您的代码,但没有成功,u