There is exe developed in C# Windows. Run this on Linux.

patrick

Well-known member
Joined
Dec 5, 2021
Messages
305
Programming Experience
1-3
Hello.
I have a question.



The EXE file developed in C# will be executed on Linux.

The contents of the EXE developed in C# Windows are CSV file save to the specified path.
==>The contents of the EXE developed in C# Windows saves a CSV file in the specified path.

The path where the csv file is saved is string savepath = r“C:\folder” <=== The path on Linux will be entered into C# string as an argument.
C#:
using (StreamWriter wr = new StreamWriter(" r“C:\folder” "))
        {
            wr.WriteLine("#TYPE,MoveSpeed");
        }

is this possible?

Please Help me.
 
Let me start of by saying that you cannot take the .EXE that you compile on Windows, and just drop it on a *nix machine and expect it to run. You will need to re-compile the code on the target OS.

The next thing I will say is that is is really bad form to be creating a brand new folder at the root of someone's C: drive on Windows. Yes, that was the convention back in the Win3.1 days through Win9x, up to WinXP. After that the accepted convention is to use the OS recommended folders. So use one of the following to get the folder:
C#:
Environment.GetFolderPath(Environment.SpecialFolder.Personal))
or
C#:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))

With that out of the way, if your code was simply:
C#:
Console.Write("Enter path for the CSV file: ");
var savePath = Console.ReadLine();
using (var writer = new StreamWriter(savePath))
{
   writer.WriteLine("#TYPE,MoveSpeed");
}
Then the user can just enter the path that is appropriate for the OS on which the program is running on. Push the problem to the user.

If your path is sort of hard coded to store in the user's folder, then you could use use Path.Combine() to build up your savePath. Something like:
C#:
var dataPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal));
var savePath = Path.Combine(dataPath, "data.csv");
using (var writer = new StreamWriter(savePath))
{
   writer.WriteLine("#TYPE,MoveSpeed");
}
 
Last edited:
*** HUGE QUOTE REMOVED ***

Your Answer Thank you.

In Linux, StreamWriter (<== C# Function ) Does it work normally ?
 
Last edited by a moderator:
There is no need to quote the entire post above yours just to say "Thank you". If you have a specific question, just quote that part.

StreamWriter works normally as appropriate on that OS. You give it paths that are appropriate for that OS so that it can create or append to that file, and then by default it will write data to that file with the encodings and line separators appropriate for that OS. There is no magic conversion of a path for one OS being converted to another OS. The default encoding and line ending can be overridden.
 
Last edited:
Let me start of by saying that you cannot take the .EXE that you compile on Windows, and just drop it on a *nix machine and expect it to run
Actually, that's not quite accurate. I've no problems compiling net (core) apps on my windows machine, transferring the output to my raspberry Pi or Mac and launching it. The process is slightly different in that one doesn't run the .exe on the nix box but instead the .dll

On windows: MyProgram.exe
On *nix: dotnet MyProgram.dll

One has to be careful not to build windows-specific behavior in. For example, use Path.Combine to build paths, rather than concatening string together with "\", do not draw with GDI etc

-

It's also possible using the mono project to run framework apps in a similar way, without re compilation on the target machine. In those cases there is no DLL, one just runs "mono MyProgram.exe"
 
The process is slightly different in that one doesn't run the .exe on the nix box but instead the .dll

Which was my point.
 
Hello.
I have a question.



The EXE file developed in C# will be executed on Linux.

The contents of the EXE developed in C# Windows are CSV file save to the specified path.
==>The contents of the EXE developed in C# Windows saves a CSV file in the specified path.

The path where the csv file is saved is string savepath = r“C:\folder” <=== The path on Linux will be entered into C# string as an argument.
C#:
using (StreamWriter wr = new StreamWriter(" r“C:\folder” "))
        {
            wr.WriteLine("#TYPE,MoveSpeed");
        }

is this possible?

Please Help me.

Yes, it's possible to run a C# EXE on Linux and write a CSV file to a specified path. You’ll need to modify your code slightly to handle the Linux file path correctly. Here’s how you can do it:
  1. Use the Correct Path: On Linux, the path will look different. For example, instead of C:\folder, you might use /home/user/folder.
  2. Modify the Code: When you pass the path as an argument, use it in the StreamWriter. Here's an example:
csharp
Copy code
C#:
using System;
using System.IO;

class Program
{
static void Main(string[] args)
    {
if (args.Length < 1)
        {
Console.WriteLine("Please provide a path.");
 return;
        }

string savePath = args[0];

using (StreamWriter wr = new StreamWriter(savePath + "/output.csv"))
        {
wr.WriteLine("#TYPE,MoveSpeed");
        }
    }
}
  1. Running the EXE: When you run the EXE on Linux, provide the path as an argument:
bash
Copy code
Code:
mono YourProgram.exe /home/user/folder
Make sure you have Mono installed on your Linux system to run the C# application. This approach will allow your program to save the CSV file in the specified directory on Linux.
4o mini
 
Last edited by a moderator:
new StreamWriter(savePath + "/output.csv"))

please use Path.Combine when building paths in code, not string concatenation. Path.Combine is operating system aware and will use eg \ on windows but / on Linux

C#:
new StreamWriter(Path.Combine(savePath, "output.csv"))

Also, it's not necessarily the case that we have to use mono. That depends on if it is a net framework or net (core) app. Netcore exists for Linux and if the app is written using it rather than framework, then it would be started like

dotnetcore AppName.dll "/home/blah"

Which is equivalent to Windows'

MyAppName.exe "/home/blah"
 

Latest posts

Back
Top Bottom