How run for example win32 console app and pass generated arguments to it from WinRt component?
The variant via the url scheme is not suitable, because there may be conflicts.
Also LaunchFullTrustProcessForCurrentAppAsync() is not suitable because the parameters must be constant in the manifest:
if (Windows::Foundation::Metadata::ApiInformation::IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0)) {
Windows::ApplicationModel::FullTrustProcessLauncher::LaunchFullTrustProcessForCurrentAppAsync();
}
Are there any other solutions?
I need something like in c#:
var installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
System.Diagnostics.Process newProcess = System.Diagnostics.Process.Start(installFolder + "\\ConsoleApp.exe", "custom args");
Related
I have developed a console application in .net core which will be distributed to clients using Windows and MacOS. One of the requirements is that the console app should start up automatically once the device boots up. I am aware that this can be achieved on MacOS by adding the executable file in System Preferences --> Users & Groups --> Login Items.
I was wondering if this can be achieved by doing this programmatically in the Main method of the console application? In windows I can register the exe file in the windows registry to achieve this:
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("My App Name", "/path/to/my/app/MyApp.exe");
Is this possible on MacOS or should this be a manual process invoked by the clients?
Found the Solution. You need to make use of Apple Script commands:
public void SetAsStartupApp()
{
var applicationName = "YourAppName";
var applicationPath = "/Applications/YourAppName.app";
var deleteError = new NSDictionary();
var createError = new NSDictionary();
// Prevent Duplicate entry in Login items
var deleteScript = new NSAppleScript($"tell application \"System Events\" \n if exists login item \"{applicationName}\" then \n delete login item \"{applicationName}\" \n end if \n end tell");
deleteScript.ExecuteAndReturnError(out deleteError);
// Create entry in Login Items
var createScript = new NSAppleScript($"tell application \"System Events\" to make login item at end with properties {{path:\"{applicationPath }\", hidden:false}}");
createScript.ExecuteAndReturnError(out createError);
// Handle Errors...
}
I am trying to reference "Windows.Networking.Connectivity" classes in my desktop application. I am basically interested in handling metered connections in my app.
Basically what I am trying to do is simple:
var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
if (connectionCost.NetworkCostType == NetworkCostType.Unknown
|| connectionCost.NetworkCostType == NetworkCostType.Unrestricted)
{
//Connection cost is unknown/unrestricted
}
else
{
//Metered Network
}
The only method I know of that allows a desktop application to reference UWP assemblies is by manually editing the project file and adding the following line to the csproj file:
<TargetPlatformVersion>8.0</TargetPlatformVersion>
Applying the code and "hack" works fine but the problem is that doing so will prevent my app from running on Windows 7 which I need to support.
I was wondering if there is a way to reference UWP assemblies in a desktop application without having to drop support for Windows 7.
And since for the time being I only want to check if a connection is metered, I am open to suggestions about how to get this information without referencing Windows assemblies.
I found a way to use reflection and call UWP methods without having to specify a target platform. For my case this is what I did:
var networkInfoType = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");
var profileType = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");
var profileObj = networkInfoType.GetTypeInfo().GetDeclaredMethod("GetInternetConnectionProfile").Invoke(null, null);
dynamic profDyn = profileObj;
var costObj = profDyn.GetConnectionCost();
dynamic dynCost = costObj;
var costType = (NetworkCostType)dynCost.NetworkCostType;
if (costType == NetworkCostType.Unknown
|| costType == NetworkCostType.Unrestricted)
{
//Connection cost is unknown/unrestricted
}
else
{
//Metered Network
}
I'm making a C# program and I need to find installation paths of some software that is installed on a computer.
What I have to work with is, I have the Program's name (e.g. Google Chrome), i have the process name (e.g. Chrome.exe).
What I need now is the path to Chrome.exe. How can i use C# to find the path if i was to pass either the program name or process name as a parameter for the search?
Actually I want to make a custom action which will find chrome.exe and invoke a link.
After that I will use the path for search chrome.exe and I want to default open a website via chrome. What should I do..?
Another option to consider is just launching the link using Process.Start() and letting the operating system use the default browser to open the link. That would likely be more what the user would expect.
In the WiX toolset, you can get that behavior for free using ShellExecute standard custom action from the WixUtilExtension.
You could try something like this
public string GetProcessPath(string name)
{
Process[] processes = Process.GetProcessesByName(name);
if (processes.Length > 0)
{
return processes[0].MainModule.FileName;
}
else
{
return string.Empty;
}
}
or you could use Linq
or you could do what you do but use linq
Process element = ( from p in Process.GetProcesses()
where p.ProcessName == "Chrome.exe"
select p ).FirstOrDefault( );
However there can be multiple process with same name .So you have to further modify the code according to your requirement.
hope this helps
I'm working on crossplatform app with Mono. I want to check permissions for RW-access to user that runs application. On NT I can use .GetAccessControl methods (on Mono throws PlatformNotSupported exception), but what to do with *nix and MacOS? Is there any crossplatfrom solutions?
Mono.Unix has UNIX specific implementations:
var ufi = new UnixFileInfo("/tmp/test.cpp");
ufi.CanAccess(AccessModes.F_OK); // is a file/directory
ufi.CanAccess(AccessModes.R_OK); // accessible for reading
ufi.CanAccess(AccessModes.W_OK); // accessible for writing
ufi.CanAccess(AccessModes.X_OK); // accessible for executing
FileSpecialAttributes sa = ufi.FileSpecialAttributes; //setuid, setgid and sticky bits
FileAccessPermissions fa = ufi.FileAccessPermissions;
FileAccessPermissions is defined as:
[Flags ()]
public enum FileAccessPermissions {
UserReadWriteExecute,
UserRead,
UserWrite,
UserExecute,
GroupReadWriteExecute,
GroupRead,
GroupWrite,
GroupExecute,
OtherReadWriteExecute,
OtherRead,
OtherWrite,
OtherExecute,
DefaultPermissions,
AllPermissions
}
How about just trying to open the file for RW and seeing if it succeeds?
File.GetAttributes(path)
http://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx
Not as much information as .GetAccessControl, but you can determine if the file is ReadOnly
I'm writing a Web Service (WCF) for my work and I'm looking for a way to
run script on demand on other machine.
We got machines that we connect from RDC, and I want to run a script on it
from another C# program.
Also, I can't seem to find a way to run an executable file on another machine from C#.
The reason why you can't find a part of the .Net framework that lets you run executables on another machine is because there isn't one.
If you want a straightfoward way of running an executable on a remote machine then you may be interested in PsExec (a Sysinternals tool released by Micrososft).
this is possible using WMI via C# (see http://www.codeproject.com/KB/cs/Remote_Process_using_WMI_.aspx) or via commandline using http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx ... but it is something you usually should NOT do - it creates several security issues to deal with...
EDIT - WMI with User/PW:
connectionoptions gives you the possibility to supply a UserName + Password - see http://msdn.microsoft.com/en-us/library/system.management.connectionoptions.aspx
I know this is an old post, but I would like to add that it is certainly possible to run a remote executable without PsExec which many virus software flags as problematic. Also most sys admins don't want PsExec on their web servers. And, running a remote executable via mapped drive or UNC does not mean that you have the license installed, so it may fail (or run a demo version) depending on the software.
The key is to wrap the System.Diagnostics.Process steps in a WCF service. Here is a partial example...
{
// Define a service contract.
[ServiceContract(Namespace = "http://MyDataServices.foo.com")]
public interface IDataService
{
[OperationContract]
bool ProcessStatTransfer(MemoryStream inputCommands, string inputName);
}
// Implement the IDataService service contract in a service class.
public class DataService : IDataService
{
// Implement the IDataService methods.
public bool ProcessStatTransfer(MemoryStream inputCommands, string inputName)
{
try
{
// StatTransferLocation is C:\Program Files\StatTransfer12-64\st.exe
// on the machine that hosts the service
string m_stattransfer_loc = ConfigurationManager.AppSettings["StatTransferLocation"].ToString();
string m_stattransfer_file = ConfigurationManager.AppSettings["CommandFiles"].ToString() + inputName;
using (FileStream m_fsfile = new FileStream(m_stattransfer_file, FileMode.Create, FileAccess.Write))
{
inputCommands.WriteTo(m_fsfile);
}
ProcessStartInfo processInfo = new ProcessStartInfo("\"" + m_stattransfer_loc + "\"");
processInfo.Arguments = "\"" + m_stattransfer_file + "\"";
processInfo.UseShellExecute = false;
processInfo.ErrorDialog = false;
processInfo.CreateNoWindow = true;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.Start();
return true;
}
catch
{
return false;
}
}
.....
Then you add a service reference and invoke the method. No mappings, PsExec, or WMI. It is a pure C# solution.