我已经在stackoverflow上写了一些文章,但我无法弄清楚如何做到这一点。我已经制作了一个用于读取混合\二进制文件的小代码(创建一个新的,使用分离的方法,较旧的一个被搞砸了) - 但我对如何在程序的主要方法上调用该方法有疑问。
我想出我应该在方法的调用中使用它们之前实例化每个变量 - 但是我如何用字符串做呢?我知道这是一个新手问题,但我是c#的新手 - 我也搜索过stackoverflow和其他地方,但找不到合适的答案。
感谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication2
{
class Program
{
public void FileOpen(ref OpenFileDialog of, ref string filename, ref int nfiles)
{
of.Filter = "PPF Files | *.ppf";
of.Multiselect = true;
of.ShowDialog();
filename = of.FileName;
nfiles = of.FileNames.Count();
}
public void FileRead(ref OpenFileDialog of, ref string filename, ref List<string> freaded, ref int nfiles, ref string filecontents)
{
string aux;
List<string> filesreaded = new List<string>();
if (string.IsNullOrEmpty(of.FileName) == false)
{
for(int i=0; i<nfiles; i++)
{
// Read the file into <bits>
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
var len = (int)fs.Length;
var bits = new byte[len];
fs.Read(bits, 0, len);
// Dump 16 bytes per line
for (int ix = 0; ix < len; ix += 16)
{
var cnt = Math.Min(16, len - ix);
var line = new byte[cnt];
Array.Copy(bits, ix, line, 0, cnt);
// Convert non - ascii characters to .
for (int jx = 0; jx < cnt; ++jx)
{
if (line[jx] < 0x20 || line[jx] > 0x7f)
line[jx] = (byte)'.';
}
aux = Encoding.ASCII.GetString(line);
filecontents += aux;
}
}//Closing FileStream
}//Closing files loop (operations for each file)
}//Closing IF Statement
}//Closing FileRead function
[STAThread]
static void Main(string[] args)
{
Program a = new Program();
OpenFileDialog openf = new OpenFileDialog();
int filenumber = new int();
string fullfilename //What to do here?
//How can i instantiate a new string so i can use it when calling the method ?
a.FileOpen(ref openf , ref fullfilename , ref filenumber);
}
}
}