嵌入资源后的C# [英] C# After embedding resources

查看:74
本文介绍了嵌入资源后的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

最近,我进入了C#.我只是C#的新手,显然我还有很长的路要走,直到我能熟练使用它.

希望我能在这里得到一些帮助.我已经在其他网站上发布了消息,他们告诉我,他们认为我正在尝试进行某种黑客之类的行为…….的确是非常错误的.

我做了一个非常漂亮的C#表单应用程序.它看起来很棒,并具有关于"表单,与我们联系"表单以及其他没有的表单(是的,有些技巧,如果他们有任何错误或问题,请让他们与我联系:/).

无论如何,它看起来很棒,我可以按照我的方式进行所有设置.问题是我对C#还是陌生的,因此高级语言非常糟糕.

我不想让该程序必须安装才能正常工作,因为安装它只是在浪费空间,而要适应它的功能,因为它作为便携式.exe ...会更加方便. />
基本上,从技术上讲,它是一款易于使用且外观美观的游戏更新程序.该游戏包含一些补丁程序,人们必须手动安装这些补丁程序,然后他们必须始终重新安装才能更改为旧版本等.此C#表单应用程序只需触摸按钮即可为他们完成此操作.

他们所需要做的就是按下button1,它将修补到该版本等.

我了解如何将资源嵌入到应用程序中.但是我需要帮助来了解如何检查PC上的程序文件目录中的游戏路径(游戏路径必须默认工作,这样该路径就不会改变,因此我可以很容易地定义路径)如果文件路径存在,那么它将删除它们在该文件夹中不同目录中的旧文件,然后我要将资源文件复制到指定路径,如果游戏目录不存在,则会出现错误消息"[code]

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]

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]".

有人可以举例说明如何执行此操作吗?人们已经告诉我要比那做得多",但是如果我不知道他们到底在干什么,那将无济于事.我知道我应该以更简单的方法开始,并且当我有更多经验时可以执行此操作,但是现在是我执行此操作的最佳时间...我希望嵌入的文件是.dll .inf .txt .png和.exe.我将不会执行任何这些文件,也不会执行任何操作,只是删除了先前的版本后将它们放置在游戏文件夹中.

希望有人能提供一些智慧.


[/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.

推荐答案

您可以使用System.IO命名空间和Directory.Exists方法来检查目录是否存在.
示例:
You can use the System.IO namespace and the Directory.Exists method to check if a directory is there.

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

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


@标记可帮助省略转义字符.

您还可以检查诸如FilePath之类的类,因为它们包含各种有用的方法.只需键入 File 和一个点即可查看方法.

C#程序还有一个替代方法,一个Inno安装脚本.我不知道这是否适合您的情况,但您可以看一下. http://www.jrsoftware.org/isinfo.php/ [


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/[^]


You can save a resource with this method:

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

SaveResource("ConsoleApplication.filename.dll", @"C:\filename.dll");



请注意,资源名称的格式为namespaceyourResourceName.



Note that the resource name is formatted as namespace dot yourResourceName.


上面的方法适用于...

但是,即使我以管理员身份运行程序并嵌入了文件,我仍然无法移动文件...


我认为启用用户帐户控制的PC不能让我移动嵌入式资源.是他们无需关闭UAC即可完成这项工作的一种方法吗?

我正在尝试将文件移动到program files \ game文件夹.您如何克服这种错误?我不介意我是否必须制作某种消息框,询问他们是否批准要移动的文件,是否可以这样做?我没有什么可隐藏的.
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.


感谢您的输入.

我现在有这个:
[代码]
Thanks for the input.

I have this at the moment:
[code]
//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.


这篇关于嵌入资源后的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆