Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hey guys,

Recently i have been getting into C#. I am only new to C# and clearly have a long way to go before i will be literate with it.

Hopefully i can get some help here. I have posted on some other sites and they tell me that they assume i'm trying to make some kind of hack or something.... They are indeed very wrong.

I have made a very nice looking C# forms application. It looks great and has "About" forms, "Contact Us" forms and what not(yeah some hack, letting them contact me if they have any errors or issues:/).

Anyways it looks great and i have everything setup they way i want it. The problem is i am new to C# so the advanced language i am terrible at.

I do not want to make this program have to be installed to work if possible as installing it is just a waste of space and to suit what it is made for it would be much more convenient as a portable .exe....

Basically its technically a easy to use nice looking Updater for a game. The game has patches which people have to manually install and after they do so they have to always reinstall to change to old versions etc. This C# form app does this for them from the touch of a button.

All they need to do is press button1 and it will patch to that version etc.

I understand how to embed a resource to the app. However i need help to understand how to check a program files directory on their PC for the game path(the game path has to be default to work so this path never changes so i can define the path easily enough) If the file path exists Then it will delete their old files which are in different directories in that folder and then i would like to copy the resource files to the specified paths, If the game directory doesnt exist it an error message will come up "[code]
C#
else
{
    MessageBox.Show("Can not find Game Install Folder. Make sure you have the game installed in the default location.",
    "Error",
    MessageBoxButtons.OK,
    MessageBoxIcon.Error,
    MessageBoxDefaultButton.Button1);
}

[/code]".

Can someone Please make an example of how to do this? I have been told by people "do this than that" But that is not going to help me if i have no idea what the hell they are on about. I know i should have started with something simpler and do this when i have more experience however now is the perfect time for me to do this... Files i am hoping to embedd are .dll .inf .txt .png and .exe. I am not going to execute any of these files or anything just place them in the games folder after deleting the previous versions.

Hopefully someone can give some wisdom.
Posted

You can use the System.IO namespace and the Directory.Exists method to check if a directory is there.

Example:
C#
using System.IO;  // remember to add this at the beginning of the code file

if(Directory.Exists(@"C:\Program Files\MyGame"))
{
...
}

The @ mark is there to help to omit the escaping characters.

You could also check classes like File and Path, because they contain all sorts of helpful methods. Just type File and a dot to see the methods.

There is also an alternative for C# program, an Inno setup script. I don't know if that's suitable for your case, but you could take a look at it. http://www.jrsoftware.org/isinfo.php/[^]

Edit:
You can save a resource with this method:
C#
public static void SaveResource(string resourceName, string fileName)
{
    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
    using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
    {
        byte[] b = new byte[stream.Length + 1];
        stream.Read(b, 0, Convert.ToInt32(stream.Length));
        fileStream.Write(b, 0, Convert.ToInt32(b.Length - 1));
        fileStream.Flush();
        fileStream.Close();
    }
    stream.Close();
}


Now you just call
C#
SaveResource("ConsoleApplication.filename.dll", @"C:\filename.dll");


Note that the resource name is formatted as namespace dot yourResourceName.
 
Share this answer
 
v3
The above works...

But even if i run the program as admin and have the file embedded i still cannot move the file...


I think that the PC i am on with User Account Control on doesn't let me move the embedded resource. Is their a way in which i can make this work without having to turn UAC off?

I am trying to move the file to program files\game folder. How do you overcome such an error? I don't mind if i have to make some kind of message box which asks them if they approve the file to be moved if that will let me do it? I have nothing to hide.
 
Share this answer
 
Comments
kornakar 11-Jul-12 8:18am    
Please don't use the "answer question" button for commenting.
Thanks for the input.

I have this at the moment:
[code]
C#
//Patch Button
        private void button2_Click(object sender, EventArgs e)
        {
            if (File.Exists(@"C:\PROGRA~1\GAME~1"))
                {
                    //Move update.exe
                    if (File.Exists(@"C:\PROGRA~1\GAME~1\PRO\patch.dll"))
                        {
                            File.Delete(@"C:\PROGRA~1\GAME~1\PRO\patch.dll"))
                        }
                    File.Copy(HOW_DO_I_DETERMINE_EMBEDDED_FILE_PATH, @"C:\PROGRA~1\GAME~1\PRO\patch.dll"
}
            else
            {
                MessageBox.Show("Can not find game install folder. Make sure you have game installed in the default location.",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
            }
        }

[/code]

Thing is, I don't think i can specify a file location for an embedded file as it isn't on their computer.. So i guess i would have to define/delete all the files and then somehow move the embedded resources to their game path. And this is where i am stuck:(

I honestly have looked at Inno setup(used it in a VB6 program). But it is like an installer maker. I wanted to embed the files and have the .exe as a portable.exe. I mean its not like its a huge program that needs .dll files or xml files to work. It just moves files from the .exe to their pc on the specified location, if the location doesn't exist it does not move the files. They can also restore the files if they wanted anyways because theirs only 2 different patches but they shouldn't need to reinstall for that if i know exactly what files are used in a patch. This will save them a lot of time even myself on my 3 pcs.
 
Share this answer
 
Comments
kornakar 10-Jul-12 7:17am    
I updated my answer. Please mark the answer as accepted if you find it helpful. Thanks! :)
burgo855 10-Jul-12 7:42am    
Thanks...

This is going to irritate you, for that i am sorry:( I am making a C# forms app not a consoleapp..

Would the above still work?

Could you use the above snippet as an example for button 1? I will happily Accept your answer if i can understand it a bit better and make it work XD
kornakar 10-Jul-12 7:49am    
Yes it will work with any application. The "ConsoleApplication" in the example is the *namespace* of the application. I don't know what's your application's namespace so I just made an example.

Just put the SaveResource method in your code and call it in your button1 event like: SaveResource("WindowsFormsApplication1.patch.dll", @"C:\progra..."). (I guess your app's namespace is "WindowsFormsApplication1" :)
burgo855 10-Jul-12 7:53am    
Cheers. i haven't tried it but this would make sense. It would be a streaming app because its embedded so i'm going to have to Thank you for it mate really appreciate your help and all.
burgo855 11-Jul-12 7:50am    
Is their any reason why it doesnt let me move the file to any locations? It wont let me do it even if i run it as an admin :/

Says something along the lines of no permissions....

im trying to place it in program files where the game is but as i sai it doesnt even work for desktop:/

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