How to get the path of the installed Excel.exe programmatically? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there any way that I could find a way to retrieve the path of the Excel executable file in C# or VB.Net? I'm aware of these two options below, but unfortunately they are not suitable. Please suggest if you have an alternative.
Using the Registry
Using Process.Start("Excel") and getting the path from the Excel process
Cannot use the Registry option because people without sufficient rights may be using my application and hence we wanted to stay away. If this assumption is wrong and Registry is reliable for any level of user access rights, please let me know.
With Second option, I'm seeing the Excel process being opened, if there is a way where I can start, but not have Excel process appear at all to the user, this may work.

I ended up with this logic and it serves my purpose. If Excel is a running process, then I'm getting the path from there, if not I'm getting by creating an instance, which doesn't actually show the Excel UI, but runs in the background.
string path = string.Empty;
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == "EXCEL")
{
path = theprocess.MainModule.FileName;
break;
}
}
if (path == string.Empty)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
dynamic xlApp = Activator.CreateInstance(officeType);
xlApp.Visible = false;
path = xlApp.Path + #"\Excel.exe";
xlApp.Quit();
}

You can start it hidden and close it. You would have to include the interop services for the below code
Dim xl As Excel.Application = New Excel.Application
xl.Visible = False
Dim Excel_Path As String = xl.Path()
xl.Quit()

Check at this link
You can start the process hidden, so the user will not see the window
startInfo.WindowStyle = ProcessWindowStyle.Hidden;

Related

How can I fix this error: System.UnauthorizedAccessException in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm doing a program that create and read .txt files, this function OpenFile should open a selected file but this error appeared. What can I do to the program have authorized access?
void OpenFile()
{
openFileDialog1.ShowDialog();
FileInfo info = new FileInfo(openFileDialog1.FileName);
string directory = info.DirectoryName;
StreamReader str = new StreamReader(directory);
string read = str.ReadLine();
LoadScreenWithText(read);
}
System.UnauthorizedAccessException: Access to the path 'C:\NotePad' was denied'
Im assuming that you are getting and absolute file path in this property "openFileDialog1.FileName"
Try running you program using "Run as administrator" by right clicking on EXE .If you are debugging it with visual studio run VS as an administrator .
Note :Good practice is to always check that whether the file exist or not before further proceeding
Use:
openFileDialog1.ShowDialog();
if(File.Exists(openFileDialog1.FileName){
//Your rest of the code
}

Playing Audio Files with spaces in filename [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
is there any possibility to play audio-files with spaces in their names?
For instance: Calvin Harris - Summer.mp3
I want to open that file in the Windows Media Player/VLC Media Player and then the error log in the VLC shows: "Bad File descriptor in C:\..Project\Release\Calvin"
Problem: The spaces!
CalvinHarris-Summer.mp3 can be opened without problems.
How could I solve that problem without renaming the audiofiles?
I'm starting the audiofile and the player with a Process.Start
If you pass the file name as an argument to the process with quotes around it, this should work for both VLC/WMP when there are blanks in the file name.
For example.
string fileIn = #"c:\Wherever\Calvin Harris - Summer.mp3";
ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
Arguments = String.Format("\"{0}\"", fileIn),
FileName = VLC_EXE_PATH
};
Process.Start(processStartInfo);

How do I keep a process open? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a web application in ASP.Net/C# (using MVS), and I want to be able to launch some programs on the client side (this should be safe since it's only for intranet use), and keep these programs open in order to give them some command inputs through the web app.
For example, I would like to open "cmd.exe" client-side, and then being able to send multiple commands one after another (synchronized with some buttons of my web form) to the process.
How can I do this? I've read a lot about using a block of Javascript with an ActiveX object, or in C# with System.Diagnostics.Process, but I'm quite stuck on how to proceed.
function RunEXE()
{
var oShell = new ActiveXObject("WScript.Shell");
var prog = "c:\\WINDOWS\\system32\\notepad.exe";
oShell.Run('"'+prog+'"',1);
}
You could also have a look at custom protocol handlers, that's what e.g. Steam uses. You'd simply have a link like myprotocol://dosomething/whatever and it will launch your client-side application with the given URL.
It's basically just about writing a registry key: http://msdn.microsoft.com/en-us/library/ie/aa767914(v=vs.85).aspx
For example, to register a protocol named alert, you can do this:
HKEY_CLASSES_ROOT
alert
(Default) = "URL:Alert Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "alert.exe,1"
shell
open
command
(Default) = "C:\Program Files\Alert\alert.exe" "%1"
This will cause the application in command to be launched when you navigate to an url that starts with alert://.
Using c#
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "CMD.EXE";
startInfo.Arguments = "list of args";
Process.Start(startInfo);
Now after that you can keep checking weather the is open or not by
Process[] processes = Process.GetProcesses();
or
Process[] chromes = Process.GetProcessesByName("chrome");
Check if you process is open or not then pass args to it

C# - Launch a process in a different directory of the executable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to launch a process that is for example in this path:
c:\A\ApplicationToBeLaunched.exe
I want that this process runs in a different path of their executable. This other folder will have all the configuration files of the ApplicationToBeLaunched.exe application. For example, the path could be:
c:\B\
I am trying to do this with this c# code:
System.Diagnostics.Process prProcess = new System.Diagnostics.Process();
prProcess.StartInfo.FileName = "c:\\A\\ApplicationToBeLaunched.exe";
prProcess.StartInfo.UserName = "";
prProcess.StartInfo.UseShellExecute = false;
prProcess.StartInfo.WorkingDirectory = "c:\\B\\";
prProcess.Start();
But the process always is executed in the application directory (c:\A\). I also have tried to set the property UseShellExecute to true.
What am I doing wrong? Can any help me?
Editing:
After some tests, I have checked that the issue is in the application launched. The process with the working directory is working fine.
i did a little test, created a small program with this in the Main method:
System.Diagnostics.Process prProcess = new System.Diagnostics.Process();
prProcess.StartInfo.FileName = #"C:\src\Test\ObjectTest\ObjectTest\bin\Release\ObjectTest.exe";
prProcess.StartInfo.UserName = "";
prProcess.StartInfo.UseShellExecute = false;
prProcess.StartInfo.WorkingDirectory = #"c:\temp\";
prProcess.Start();
And in the ObjectTest.exe
Console.WriteLine(Directory.GetCurrentDirectory());
And the output result:
c:\temp\
So i think your having some other problem with your program and not problem with the WorkingDirectory.
I don't know why WorkingDirectory is not working but I have encounter this by myself in the past.
Try to change the current working directory before starting the new process:
Directory.SetCurrentDirectory("c:\\B\\");

Is it possible to create an application to check every opened file on windows? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'd like to build an application (if there isn't already one) to log every Windows (7, 64-bit) opened file (including attributes) on C#.
What do you think? Could it be possible? Is there another software already built to do this? Do I Need to use a low-level language (C?) and manage kernel events?
I mean, if I open this c:/programs/file.exe /my-attributes how can I catch /my-attributes
What you are really asking for is the command line that was used to start a process. An entirely different problem from trying to discover what files are opened by a process. And one that you can actually do with C#, using System.Management to run a query on the Win32_Process class. Start a new console mode project and add a reference to System.Management. Make the code look like this:
using System;
using System.Management; // NOTE: add a reference to System.Management!
class Program {
static void Main(string[] args) {
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Process");
foreach (ManagementObject queryObj in searcher.Get()) {
Console.WriteLine(queryObj["CommandLine"]);
}
Console.ReadLine();
}
}
Beware that the output is not very clean and requires parsing. Some processes may appear with their internal Windows path (like \??\etc), some processes don't have a full path name. Review the Win32_Process class documentation to see what other properties are available.

Categories