Bernice
Bernice

Reputation: 2592

How can I include saving in a text file in executable file?

I have an application in Visual Studio C# which includes saving into a text file, how can I have a .exe sent to another computer and not have an exception in saving?

I need to send a .exe file by email (Yes it's possible) and this application includes saving the state of the game. How can I send this .exe file and let the user be able to save in his computer please?

The problem is that when I send my application's executable file on another computer, I'm getting an exception in saving. Because on the other computer I don't have the text file which I'm saving the game.

I am saving here :

StreamWriter myFile = File.CreateText(Directory.GetCurrentDirectory()+"//ConnectFour.txt");

in the obj/Debug/ of the project..

Thanks for your help :)

Upvotes: 0

Views: 2255

Answers (4)

rezidual
rezidual

Reputation: 31

You probably do not have sufficient privileges to save the file on the remote machine. If you give us more information on the exact exception that is being thrown (type of exception, message, stack trace, etc) you will get a more accurate answer.

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

The problem when sending executables by email are the anti-virus-scanners. Some of them refuse e-mails containing executables. Others accept the mails but delete the attachment.

The solution consists in hiding the executable. This can be done by zipping the executable and sending the zip-file.

Often this is enough to solve the problem but some anti-virus-scanners are very smart and even recognize executables within the zip-attachment. The solution here is to encrypt the zip-file with a password. I often just use the password "pwd" and mention it in the e-mail text. The anti-viruses are not (yet) smart enough to understand this text.


UPDATE

Now I understand your problem. It has nothing to do with sending the executable.

An application can determine from which directory it has been started like this

string dir = System.IO.Path.GetDirectoryName(
    System.Windows.Forms.Application.ExecutablePath
);

An alternative is (if you don't have a reference to WinForms):

string dir = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location
);

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Most likely current location is not writable by current user.

Using "current directory" is dangerous as you have no control over where application is launched from. It is very useful for command line utilities, but not so much for regular windowed applications. Use location that you know you can save files to - i.e. "My Documents".

var filePath = Path.Combine(
   Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\",
   ConnectFour.txt");
using(var  myFile = File.CreateText(filePAth))
{
  // save data here.
}

Upvotes: 1

Khan
Khan

Reputation: 18152

Sending an executable should work just fine.

Make sure the other computer has the appropriate Microsoft .NET Framework installed.

Latest framework installer: MSDN

Also, make sure the path inwhich you're saving the file to exists on the remote computer. For example, if you're trying to save to the D:\ drive and it doesn't exist. You will get an exception.

Upvotes: 2

Related Questions