Spawing exe not inheriting permissions - c#

I have an exe (test.exe) which is a 32 bit written in c++ (unicode charset). This exe will perform the restore operation. The steps include:
check outlook installation and load msmapi32.dll
create a subfolder in inbox of the specified user.
importing eml file into the subfolder.
These operations are done using temporary user who has given full access permission.
This exe is executing fine. The issue is when I try to spawn this exe using C# Process.start(), it fails with [MAPI_E_FAILONEPROVIDER] while calling "createstoreentryid".
Any suggestions please.

using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
{
if (unc.NetUseWithCredentials(path,
userName,
domain,
passWord,textFile))
{
System.Diagnostics.Process.Start(path);
}
}

Related

How to attach file to windows service?

Is it possible to attach a flat file to windows-service?
My windows-service uses few flat file (for data purposes). Usually, if it's a normal executable I would place those files in the same directory as exe. How can I achieve that with windows-service?
I've done some research on this, but all I found was:
1. Pass a path to those files as a parameter to windows-service.
2. Make a fixed path and just require those files there
But I don't like those solutions. I was wondering if it's possible to attach those files to the windows service while installing it?
How about adding these files inside the project as Embedded Resources? They won't show up on the disk, but you could still properly read them from inside the assembly itself.
Here's some reference: https://support.microsoft.com/en-ie/kb/319292
You can look up the directory that your application is installed in at runtime, using the Application.StartupPath property from System.Windows.Forms. This works for both applications and services. You should avoid hard-coding the path that you think your application is installed in, because the end user may have installed it somewhere else. You should also avoid using the current directory (i.e., opening the file by name only, without a specific path) because the user may be running your application with a different current directory.
Note that installutil does not make a copy of your service executable. So the service runs from the same directory that it was in when you installed it, and any files you place in that directory should still be there when the service is running.

batch works fine but "windows cannot find devcon.exe" in c# program

I have a batch file which disables and enables some driver using windows devcon.
when I run this batch file by double-clicking on it it works fine.
However, I tried to run it from a C# program that I wrote using this line:
System.Diagnostics.Process.Start("C:/*path to file*/file.bat");
it runs fine until it tries to open the devcon.exe and I get the following message:
after that it continues to run smoothly.
any ideas why it doesn't work from the C# program?
p.s
I can't post the batch code due to IP issues...
The problem is - as often - the "working directory". When you double-click something in the Explorer, the working directory is set to the current folder, so from the batch file's point of view it's current directory is its own directory.
When you execute a C# application, usually the working directory is the directory of the application's exe file, but not necessarily (for example if the application is run using a link, you can specify a different working directory). That's why, to find the application EXE file's directory it is not save to use GetCurrentDirectory.
So what happens is that the application runs the batch file, but with the application's directory, not the batch file's directory, as working directory. An alternative to an explicit cd within the batch file would be to specify the working directory when calling Process.Start.
ok, after a little bit of research I found this simple solution:
simply changing to the directory of the devcon.exe (using cd command) at the beginning of the batch code, i.e:
cd "C:/*path to the directory of devcon.exe*"
#rest of the code

How should I configure paths to other exe files in my project?

I am working on a project that requires a .exe file to be executed by the application. The problem here is that I cannot guarantee that the file will always be in the same location on the user's machine, my app has no way of knowing where this .exe file is located.
So for example, my application needs to execute mongod.exe which is located (by default) in the following location: C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe.
The problem here is that this is not always the case, the user might have installed this application to a different place, so I can't assume that the path to the .exe file is always the same on every client.
Currently I am using the Settings.settings file to store a default file path to the file:
The user can then either edit the app.config file or I could provide a UI to allow the user to change the file path.
So my question is, is there a better way?
Searching the path of the Mongodb.exe file in Registry can be useful. Try to find the exe path from the Registry entry.
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\MongoDB\MongoDB Connector");
if (key != null)
{
Object o = key.GetValue("InstallPath");
}
Object o will give the path of MongoDB.exe file.
P.S.: If your application does not have rights to access the local machine's registry, then this approach won't work

Application creating / deleting local xml files when installed

I have an application that needs to create, read and delete 2 xml files. These files are stored in the same directory as the application file. When I run this application in debug it works fine. but as soon as you create an installer for this and run the installed program it failed to create the files. Is there any way I can set the application permission to the folder it has been installed to?
Any help would be great.
Thank you
Here is the code im using:
if (File.Exists("ApplicationData.xml") == true)
{
File.SetAttributes("ApplicationData.xml", FileAttributes.Normal);
File.Delete("ApplicationData.xml");
}
doc.Save("ApplicationData.xml");
This is throwing a System.UnauthorizedAccessException
Rather than start changing directory permissions, how about putting the files into the temporary directory? You can get the directory from Path.GetTempPath().
I would suggest you to create a "tmp" folder from your application, and then create your xml files inside . This will guarantee you that have permission on the folders, as you have created them in the privilege scope of the application.
string folderName = #"Folder";
string pathString = System.IO.Path.Combine(folderName, "SubFolder");
System.IO.Directory.CreateDirectory(pathString);
Here i've tested it out for you:
http://pastebin.com/cAmFQvee
This works fine

Adding Files/Folders to Program Files x86 Folder with C#

I am trying to create a file directory (folder) in the 32 bit Program Files folder to store data that a user will be creating in the program. However, it keeps saying that access is denied when I try to do so. Is there anyway to give the program permission to access the 32 bit program files folder and add subsequent folder and files to it? Here is my code below which is producing a run-time error due to not having permission.
string mainDirectory=Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)+"\\UKC Trial Reporter Files";
if (!Directory.Exists(mainDirectory))
{
Directory.CreateDirectory(mainDirectory);
}
Store your data in Environment.SpecialFolder.ApplicationData, Environment.SpecialFolder.LocalApplicationData or Environment.SpecialFolder.CommonApplicationData (if data have to be accessible by any user).
Never store it in %ProgramFiles%.
%ProgramFiles% is intended for... program files: executables, dlls, app.config files.
In addition of security reasons, there's yet another reason to do this never: when uninstalling, your application's installer must sweep all of files and folders it had created. And this is impossible, when list of files in installdir was changed during application lifetime.
Fortunately, we have UAC since Windows Vista has released.
Don't do that.
Program FIles is exactly that; it should contain EXEs and static resources.
Instead, you need to write to the user's SpecialFolder.ApplicationData folder.

Categories

Resources