我正在学习C#编程语言。有人可以帮我吗?我尝试添加字符串(栅格地图的目录)。
string Raster = @“ C:\ Raster.tif”;
如何将其添加到数组中以便可以传递给以下代码:
数据集ds = Gdal.Open(args [0],Access.GA_ReadOnly);
class GDALReadDirect {
public static void usage()
{
Console.WriteLine("usage: GDALDatasetRasterIO {GDAL dataset name} {output file name}");
System.Environment.Exit(-1);
}
public static void Main(string[] args)
{
if (args.Length < 2) usage();
// Using early initialization of System.Console
Console.WriteLine("");
try
{
/* -------------------------------------------------------------------- */
/* Register driver(s). */
/* -------------------------------------------------------------------- */
Gdal.AllRegister();
/* -------------------------------------------------------------------- */
/* Open dataset. */
/* -------------------------------------------------------------------- */
Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly );
if (ds == null)
{
Console.WriteLine("Can't open " + args[0]);
System.Environment.Exit(-1);
}
Console.WriteLine("Raster dataset parameters:");
Console.WriteLine(" Projection: " + ds.GetProjectionRef());
Console.WriteLine(" RasterCount: " + ds.RasterCount);
Console.WriteLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");
/* -------------------------------------------------------------------- */
/* Get driver */
/* -------------------------------------------------------------------- */
Driver drv = ds.GetDriver();
if (drv == null)
{
Console.WriteLine("Can't get driver.");
System.Environment.Exit(-1);
}
Console.WriteLine("Using driver " + drv.LongName);
/* -------------------------------------------------------------------- */
/* Processing the raster */
/* -------------------------------------------------------------------- */
SaveBitmapDirect(args[1], ds, 0, 0, ds.RasterXSize, ds.RasterYSize, ds.RasterXSize, ds.RasterYSize);
}
catch (Exception e)
{
Console.WriteLine("Application error: " + e.Message);
}
}
修改
如果我将args [0]从代码更改为Raster:
string Raster = @“ C:\ Raster.tif”;
数据集ds = Gdal.Open(args [0],Access.GA_ReadOnly);
,但不会为args 1执行以下代码。 如何将地图传递给代码?
答案 0 :(得分:4)
编译项目时,将生成I am a <span class="h">x</span> with <span class="l">y</span> and also with <span class="m">z</span> which i want to replace.
。打开控制台并浏览到包含.exe的文件夹。
您现在可以启动程序并使用参数。例如:
.exe
您现在可以使用args [0]获取字符串c:\someFolders\bin\debug>myProgram.exe "C:\Raster.tif"
您可以在应用程序中使用许多参数。例如,下面的代码将显示传递给应用程序的所有参数。
"C:\Raster.tif"
使用这些参数执行时:
public static void Main(string[] args)
{
foreach (var arg in args)
Console.WriteLine(arg);
}
它将输出:
42
一个字符串
不同
单词
你好世界!