Click here to Skip to main content
15,991,072 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
[Serializable]
class SerializableNifti
{
	private ushort[] _data;

	public ushort[] Data => _data;

	public int[] Dimensions => _dimensions;

	private int[] _dimensions;
	public SerializableNifti(ushort[] data, int[] dimensions)
	{
		 _data = data;
		 _dimensions = dimensions;
	}
}

namespace NiftiNET
{

	
	class Program
	{

		public static void JsonSerialize<T>(T c, string path)
		{
			// var sr = new StreamReader(s);
			// var data = sr.ReadToEnd();
			string jsonString = JsonSerializer.Serialize(c);	
			
			if (File.Exists(path))
				File.Delete(path);
			File.WriteAllText(path, jsonString);
		}
		public static void SerializeNow<T>(T c, string path) {  
			// File f = new File("temp.dat");  
			// Stream s = f.Open(FileMode.Create);  
			if (File.Exists(path))
				File.Delete(path);
			Stream s = File.Create(path);
			BinaryFormatter b = new BinaryFormatter();  
			b.Serialize(s, c);  
			s.Close();  
		}  
		public static T DeSerializeNow<T>(string path) {  
			// File f = new File("temp.dat");
			Stream s = File.OpenRead(path);
			// Stream s = f.Open(FileMode.Open);  
			BinaryFormatter b = new BinaryFormatter();  
			var c = (T) b.Deserialize(s);  
			// Console.WriteLine(c.name);  
			s.Close();
			return c;
		}  
		
		
		static void Main(string[] args)
		{

			string inPath = @"C:\Users\User\Desktop";

            if (args.Length > 0)
				inPath = args[0];
			else
			{
				Console.WriteLine("Please give path to nii file");
				return;
			}


            Console.WriteLine($"Reading... From {inPath}");
			Nifti.NET.Nifti nifti = null; 
			if (!inPath.EndsWith("ole"))
				 nifti = NiftiFile.Read(inPath);
			Console.WriteLine($"Completed Nifti generation from {inPath}");

			var outPath = string.Empty;
			
			if (args.Length >= 2)
				outPath = args[1];

			var filetype = inPath.Split(".")
				.Skip(1) // First part is not filetype
				.Where(s => !int.TryParse(s, out _)) // ".01." is subversion and should be part of filename
				.Aggregate((acc, s) => $"{acc}.{s}");
			var filename = inPath.Split("\\/".ToCharArray()).Last().Replace("." + filetype, ""); 
				
			var jsonPath = $"{outPath}{filename}.json";
			
			if (outPath.EndsWith("json", StringComparison.InvariantCultureIgnoreCase))
				jsonPath = outPath;


I try to implement again the old C# .nii(nifti) file source.
But something strange~

string inPath = @"C:\Users\User\Desktop";

            if (args.Length > 0)
				inPath = args[0];
			else
			{
				Console.WriteLine("Please give path to nii file");
				return;
			}


For this implement,
string inPath = "C:\Users\User\Desktop";
was default. But after running my code,
surely good .nii (nifti) file exist but...
no matter how I've tried to correct to others..
Just directly go for
else
{
Console.WriteLine("Please give path to nii file");
return;
}
like manually CMD .exe running below output.

C:\Users\User\source\test\NevrolensNiftiTool\bin\x64\Debug\net5.0>NevrolensNiftiTool.exe
Please give path to nii file

C:\Users\User\source\test\NevrolensNiftiTool\bin\x64\Debug\net5.0>NevrolensNiftiTool.exe
Please give path to nii file

What's the problem?

What I have tried:

1. I've tried to change
string inPath = "C:\Users\User\Desktop";

to
var inPath = "C:\Users\User\Desktop";

or
var inPath = @"\\C:\Users\User\Desktop";

string inPath = @"\\C:\Users\User\Desktop";

string inPath = @"\\C:\Users\User\Desktop\xxx.nii";

string inPath = "C:\Users\User\Desktop\xxx.nii";

var inPath = "C:\Users\User\Desktop\xxx.nii";

Verbatim strings~
But failed and same message "Please give path to nii file"
Posted
Updated 10-Jul-24 23:42pm
v2

Quote:
C#
if (args.Length > 0)
    inPath = args[0];
else
{
    Console.WriteLine("Please give path to nii file");
    return;
}
NevrolensNiftiTool.exe
Please give path to nii file
The args parameter contains the command-line arguments passed to the application.

When you run NevrolensNiftiTool.exe, you are not passing in any command-line arguments. Therefore, args.Length == 0, and you get the error message.

Either pass in the path on the command-line:
NevrolensNiftiTool.exe "C:\Users\User\Desktop"
or change your code so that it uses a default path if no command-line arguments are provided.
 
Share this answer
 
The error you are seeing has nothing to do with inPath. Your code is checking to see if there are any command line arguments and, if not, you are writing the error out and exiting. Remove these lines:
C#
else
{
  Console.WriteLine("Please give path to nii file");
  return;
}
That fixes the first problem. The second problem is that you are pointing to a folder in the inPath portion. You aren't actually pointing to the nii file in your code, so setting that would be some value. Even better, would be to introduce a check to make sure the file ends in nii and exists.
C#
private bool NiiFileIsPresent(string path)
{
  return path.ToLowerInvariant().EndsWith("nii") && File.Exists(path);
}
Then, just call this method to check that you have a nii file.
 
Share this answer
 
Comments
Member 16143694 11-Jul-24 22:21pm    
Okay. Thank you.
I just deleted else function. And for your recommended NiiFileIsPresent(), not yet implemented.
Because this code is not nii file viewer (Just transfered to XXXXX.json file)
Anyway, Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900