0

I would like to import to registry a .reg file that exist in project resources.

The way to import a reg file uses the path to the reg file:

Process proc = new Process();  
proc = Process.Start("regedit.exe", "/s " + "path\to\file.reg"); 

Is it possible to do so with a file from resources? how do I get its path?

user3165438
  • 2,631
  • 7
  • 34
  • 54
  • If there isn't too much data in the .reg-file I would suggest to use the Microsoft.Win32-namespace to create the desired registry keys/values in code. – Håkan Fahlstedt Nov 11 '14 at 11:24
  • @HåkanFahlstedt, Good advice, Thanks, but I would like first to try import it whole. – user3165438 Nov 11 '14 at 11:25
  • When it's in your resources, it's in your assembly and doesn't have its own file name. I'd create a temporary file, write the resource to that and import it using regedit. – Hans Kilian Nov 11 '14 at 11:39

3 Answers3

0

If it is in the project folder. i-e. The folder in which the project is runnung. you can access it directly : Process.Start("regedit.exe", "/s " + "Filename.reg");

you can get the current path by using

string path =System.AppDomain.CurrentDomain.BaseDirectory; // this will give u the path for debug folder

or

string projectPath= Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())); //This will give u the project path.

you can use both and navigate arround to get the path to your desired folder. eg if you want to access a file in the Resource folder inside the project folder u can use projectPath+"\\Resource\\filename.reg"

0

If the file is embedded resource (and as such is not created on disk), it is best to read it like this and save it to a temporary file using:

var path = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), "reg");
Process.Start(path);

It may not be necessary to change the extension if you don't start the file directly but use Process.Start("regedit", "/s " + path) like you described in your question. Keep in mind that the file path should be escaped so it's parsed properly as the command line argument, temporary file path, though, will not contain spaces, so it should be okay.

Community
  • 1
  • 1
Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
0

This is not tested code, but you get the steps I hope:

Process proc = new Process();

proc.StartInfo.FileName = "regedit.exe";
proc.StartInfo.Arguments = "/s";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();

StreamWriter stdin = myProcess.StandardInput;

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "<regfile>";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    stdin.Write(reader.ReadToEnd());
}
Håkan Fahlstedt
  • 2,040
  • 13
  • 17