-2

I have an application which includes saving state of the game in a text file. I am sending .exe file to a couple of my friends. The saving is working on some computers and giving an exception on others.

When I researched a bit, I figured that this may be a problem of not having compatible .NET Framework on the receiver end. Of course, this is undesirable for me as I want the application to work on every computer..

Do you have any idea how I can make the program independent of Microsoft's version of Framework.

I am saving the file using this process, if it helps to know what I'm talking about:

string filePath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\ConnectFour.txt";

StreamWriter myFile = File.CreateText(filePath);

What can I do please?

Thanks a lot!

Bernice
  • 2,552
  • 11
  • 42
  • 74
  • 6
    All the classes/methods you are using in your code are supported from .Net framework 1.1 to 4.5. I don't think its an issue with .Net framework, rather its an issue with file path, which would be different on each computer. You may post the exception you are getting – Habib Dec 11 '12 at 12:25
  • 7
    What exceptions araise? – SynerCoder Dec 11 '12 at 12:25
  • @Habib that's why I used the method above to get the file path. It's meant to get the path of where the user puts the executable file, no? The exception they're getting is because they can't create the file – Bernice Dec 11 '12 at 12:30
  • 2
    My first thought wonders if the program is writing to a directory that it has rights for or not. – kenny Dec 11 '12 at 12:30
  • "giving an exception" with no info about the exception details is like saying "fall down and go boom when I run it". IOW, it's absolutely meaningless. The *very next thing* you should type after `exception` is information about the type of exception you get, including the error message. – Ken White Dec 11 '12 at 12:30
  • 1
    You can set the level of the version of framework inside the project settings, this however doesnt help you against case where the target computer has no framework at all or has version bellow the minimum you require. If you cannot rely on users to download required version themselves you will have to provide as installation setup following my help you. http://stackoverflow.com/questions/6090913/make-an-installation-program-for-c-sharp-applications-and-include-net-framework However as others have commented, without exception to see whats the problem this might not be the cause. – drk Dec 11 '12 at 12:34
  • I can't say what'a the exception.. since I am catching the general exception Exception in my code.. so any exception that's being fired is being caught there and displays a message box: Cannot save to file.. since it's working on my computer and not working on others I can't debug to see what's the exception being thrown! – Bernice Dec 11 '12 at 12:41
  • @studentProgrammer: That is exactly why catching the general `Exception` is an incredibly bad idea. – O. R. Mapper Dec 11 '12 at 12:51
  • I know it's a bad idea.. you're right! but I am still new to programming and I have no idea which exceptions I have to catch for example in this case of file handling :/ – Bernice Dec 11 '12 at 12:53
  • @studentProgrammer - So learn? The solution to this problem is to tell your friends to install the require version of the .NET Framework. Your answer doesn't have a good answer to it. I will be honest your current code is cleaner then any other solution that gets the results you want. – Security Hound Dec 11 '12 at 13:00

2 Answers2

2

You can include .net framework in you assembly or installer. Check this: Add .Net Framework 4.0 into setup project

Community
  • 1
  • 1
Evgeny Bychkov
  • 322
  • 1
  • 2
  • 11
2

It is more likely to be due to the permissions that individual users have to write files to that folder.

Instead, consider using the temporary file area. Use this to get it:

string filePath =  Path.Combine(Path.GetTempPath(),"ConnectFour.txt");
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • hmm yes this makes sense I think, however I'm talking to my friend (saving is not working on her computer) and she said that the text file was created, yet left empty.. so I don't think it has something to do with permissions, aye? – Bernice Dec 11 '12 at 12:35
  • 1
    Well, the code you have shown above does not show anything that actually writes to the file so it is hard to say! – Justin Harvey Dec 11 '12 at 12:39