我正在尝试构建一个示例Android应用程序,该应用程序检索此处提供的文件路径的文件信息代码
有2个.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();
} }
}
如果我构建这个我得到错误为
&#34;名称&#39; fileName&#39;在当前上下文中不存在&#34;
请帮助我
答案 0 :(得分:0)
您正在评论文件名:
//string fileName = @"";
其次,如果您想要命令行中的文件名,请使用:
public static string Main (string fileName)
{
FileInfo fi = new FileInfo(fileName);