Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using files that are located under the project?

Using files that are located under the project?

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tkrn
    wrote on last edited by
    #1

    I have a two files lets say File1.BMP and File2.TXT which i want to write on the hard drive. How do i access those files that are included the project? I am looking to have these two files to be compiled into the executable and be extracted at a certain point in my application. I've tried looking around but have not found that much. Thank you in advance.

    D realJSOPR 2 Replies Last reply
    0
    • T tkrn

      I have a two files lets say File1.BMP and File2.TXT which i want to write on the hard drive. How do i access those files that are included the project? I am looking to have these two files to be compiled into the executable and be extracted at a certain point in my application. I've tried looking around but have not found that much. Thank you in advance.

      D Offline
      D Offline
      Dr Emmett Brown
      wrote on last edited by
      #2

      You can add them to resources. All you have there is embedded in your app executable. However you can't easily extract them to a folder. You can use an installer to do that. To add anything to resources just go to the properties folder under your project (in the solution explorer) and edit the Resources.resx file. You can add existent or new files. After you add them you can access them through Properties.File1 for example. Cheers,

      rotter

      realJSOPR C 2 Replies Last reply
      0
      • D Dr Emmett Brown

        You can add them to resources. All you have there is embedded in your app executable. However you can't easily extract them to a folder. You can use an installer to do that. To add anything to resources just go to the properties folder under your project (in the solution explorer) and edit the Resources.resx file. You can add existent or new files. After you add them you can access them through Properties.File1 for example. Cheers,

        rotter

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        rotter512 wrote:

        However you can't easily extract them to a folder.

        Wrong, mooselips...

        ResourceManager rm = new ResourceManager("AppName.Properties.Resources", Assembly.GetExecutingAssembly());
        
        // and if the viewer swf file doesn't already exist in the folder
        byte\[\] stream = (byte\[\])rm.GetObject("MyResource");
        string filename = "myresource.bmp";
        
        if (stream != null)
        {
        	// and write the file to disk
        	try
        	{
        		using (FileStream streamTarget = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
        		using (BinaryWriter writer = new BinaryWriter(streamTarget))
        		{
        			// make the file hidden when it's created on the hard drive (included because it's cool)
        			File.SetAttributes(filename, File.GetAttributes(viewerFile) | FileAttributes.Hidden); 
        			// write the contents
        			writer.Write(stream);
        		}
        	}
        	catch (Exception ex)
        	{
        		if (ex != null) {};
        	}
        }
        

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        modified on Monday, August 25, 2008 11:18 AM

        C 1 Reply Last reply
        0
        • T tkrn

          I have a two files lets say File1.BMP and File2.TXT which i want to write on the hard drive. How do i access those files that are included the project? I am looking to have these two files to be compiled into the executable and be extracted at a certain point in my application. I've tried looking around but have not found that much. Thank you in advance.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          Look at my reply to rotter512... Once you add the files as resources and compile the app, it's a piece of cake to extract them at runtime.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          • realJSOPR realJSOP

            rotter512 wrote:

            However you can't easily extract them to a folder.

            Wrong, mooselips...

            ResourceManager rm = new ResourceManager("AppName.Properties.Resources", Assembly.GetExecutingAssembly());
            
            // and if the viewer swf file doesn't already exist in the folder
            byte\[\] stream = (byte\[\])rm.GetObject("MyResource");
            string filename = "myresource.bmp";
            
            if (stream != null)
            {
            	// and write the file to disk
            	try
            	{
            		using (FileStream streamTarget = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            		using (BinaryWriter writer = new BinaryWriter(streamTarget))
            		{
            			// make the file hidden when it's created on the hard drive (included because it's cool)
            			File.SetAttributes(filename, File.GetAttributes(viewerFile) | FileAttributes.Hidden); 
            			// write the contents
            			writer.Write(stream);
            		}
            	}
            	catch (Exception ex)
            	{
            		if (ex != null) {};
            	}
            }
            

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            modified on Monday, August 25, 2008 11:18 AM

            C Offline
            C Offline
            chaiguy1337
            wrote on last edited by
            #5

            He said easily. :P

            “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

            realJSOPR 1 Reply Last reply
            0
            • D Dr Emmett Brown

              You can add them to resources. All you have there is embedded in your app executable. However you can't easily extract them to a folder. You can use an installer to do that. To add anything to resources just go to the properties folder under your project (in the solution explorer) and edit the Resources.resx file. You can add existent or new files. After you add them you can access them through Properties.File1 for example. Cheers,

              rotter

              C Offline
              C Offline
              chaiguy1337
              wrote on last edited by
              #6

              You can also add the files directly to the project and set their build type to "embedded resource". I'm not sure if that results in the same thing or something different, but they'll definitely be embedded in the file.

              “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

              1 Reply Last reply
              0
              • C chaiguy1337

                He said easily. :P

                “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                That *is* easy.

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                C 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  That *is* easy.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  C Offline
                  C Offline
                  chaiguy1337
                  wrote on last edited by
                  #8

                  Anything dealing with streams is, imo, not "easy". "Possible" would be more appropriate. To me, easy would be: ResourceManager.CopyToFile( filePath ); But I guess you meant easy for you.

                  “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups