名称';fileName';在当前上下文中不存在
本文关键字:上下文 不存在 fileName 名称 | 更新日期: 2023-09-27 18:24:42
HI我正在尝试构建一个示例android应用程序,该应用程序检索此处提供的文件路径的文件信息
有两个.cs文件,其中一个是"mainactivity.cs",其代码显示在下面
mainactivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace ACCESSTIME
{
[Activity (Label = "ACCESSTIME", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
EditText filepathText = FindViewById<EditText>(Resource.Id.FilePathText);
Button getinfobutton = FindViewById<Button> (Resource.Id.GetInfoButton);
string fileinfo = string.Empty;
getinfobutton.Click += (object sender, EventArgs e) =>
{
fileinfo = GetInfo.Main(filepathText.Text);
};
}
}
}
而另一个文件是getinfo.cs
using System;
using System.IO;
using System.Text;
public static class GetInfo
{
public static string Main (string text)
{
// Full file name
//string fileName = @"";
FileInfo fi = new FileInfo(fileName);
// Get File Name
string justFileName = fi.Name;
Console.WriteLine("File Name: {0}", justFileName);
// Get file name with full path
string fullFileName = fi.FullName;
Console.WriteLine("File Name: {0}", fullFileName);
// Get file extension
string extn = fi.Extension;
Console.WriteLine("File Extension: {0}", extn);
// Get directory name
string directoryName = fi.DirectoryName;
Console.WriteLine("Directory Name: {0}", directoryName);
// File Exists ?
bool exists = fi.Exists;
Console.WriteLine("File Exists: {0}", exists);
if (fi.Exists)
{
// Get file size
long size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);
// File ReadOnly ?
bool IsReadOnly = fi.IsReadOnly;
Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);
// Creation, last access, and last write time
DateTime creationTime = fi.CreationTime;
Console.WriteLine("Creation time: {0}", creationTime);
DateTime accessTime = fi.LastAccessTime;
Console.WriteLine("Last access time: {0}", accessTime);
DateTime updatedTime = fi.LastWriteTime;
Console.WriteLine("Last write time: {0}", updatedTime);
Console.ReadLine();
} }
}
如果我构建这个,我会得到错误为
"当前上下文中不存在名称‘fileName’"
plz帮我解决
您正在注释文件名:
//string fileName = @"";
其次,如果你想从命令行获得文件名,那么使用:
public static string Main (string fileName)
{
FileInfo fi = new FileInfo(fileName);