C# application.exe not working in Win10,but it worked on my Win7. I tried to debug in Win10 it shows me this error which is correct in win7.
using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string fullComputerName = Environment.MachineName;
//Create a Folder Path
string createFolderPath = #"C:\\Users\\" +fullComputerName+"\\Documents\\Cheques";
//Create a File Inside of a Folder
string createTxtFile= createFolderPath + "\\ChequeForDeposit.TXT";
try
{
if(!Directory.Exists(createFolderPath))
{ return; }
Directory.CreateDirectory(createFolderPath);
}
catch { }
finally { }
if(!File.Exists(createTxtFile))
{ File.Create(createTxtFile); }//The error is here
}
}
}
when i check in my win7 pc, it creates a folder and a text file. But not in Win10. it is so strange.
Your try/catch does not ensure that directory folder will exist (may generate an exception when you try to create the folder). So before creating the file, check immediately folder is exists.
Your condition is incorrect. If folder not exists then you should return, otherwise create.
try
{
if(Directory.Exists(createFolderPath) && !File.Exists(createTxtFile))
{
File.Create(createTxtFile);
}
}
Also check the permission issue. e.g. Check the permissions for the desktop folder. In Windows Explorer right click on the desktop folder, select properties and there go to the security tab. You should have write permission for that folder.
i think the folder C:\Users\ is Protected system folder by design. either you have to run as Administrator or create the file in some other drive, for eg:
#"D:\Users\" +fullComputerName+"\Documents\Cheques"
Related
I have a C# .NET project and a VS Setup Project to install it. Inside the C# .NET project I have System.Configuration.Install.Installer subclass. The Installer subclass needs to write a file inside the "c:\Program Files (x86)\AppName" folder and this file will have content from a parameter asked for in the installer.
This all works great until the user decides to log out and another user logs on.
Then the file disappears and the program does not work. I need the file to be saved in the app's Program Files (x86) folder permanently and it should not be removed.
I read that this is related to Windows Vista and later have something called a Virtual Store for Program Files but I don't understand how I can bypass this so I can really place a file there. In most cases, it would be incorrect to store in this directory, but in this case, I really need to. It is a setting made in the Installer and should be there. It is the correct place.
Below is the code of the Installer class
EDIT:
Now I just found something I did not notice before. Seem like when I log in to the other user, the installer is executed again immediately after login, but now there is no Context.Parameters["ActivationCode"] this time. IS this expected? What is actually starting the installer? Does it do this for every user? Since it fails I guess it will rollback and that removes the files.
public partial class ProjectInstaller : System.Configuration.Install.Installer {
public ProjectInstaller() {
InitializeComponent();
}
public override void Install(IDictionary stateSaver) {
base.Install(stateSaver);
if (string.IsNullOrWhiteSpace(Context.Parameters["ActivationCode"])) {
throw new InstallException("Missing activation code!");
}
string installDir = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
File.WriteAllText(installDir + "\\ActivationCode.txt", Context.Parameters["ActivationCode"]);
}
public override void Uninstall(IDictionary savedState) {
base.Uninstall(savedState);
try {
string installDir = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
File.Delete(installDir + "\\ActivationCode.txt");
}
catch (Exception) {
}
}
public override void Rollback(IDictionary savedState) {
base.Rollback(savedState);
try {
string installDir = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
File.Delete(installDir + "\\ActivationCode.txt");
}
catch (Exception) {
}
}
}
I am trying to add some command line arguments to my application using CommandLineParser:
using CommandLine;
using System;
namespace ConsoleApplication1
{
class Options
{
[Option('s', "site", Required = true,
HelpText = "The site to connect to. Please include http://")]
public string sitename { get; set; }
[Option('l', "list", Required = true,
HelpText = "The list to connect to.")]
public string listname { get; set; }
}
class Program
{
static void Main(string[] args)
{
var options = new Options();
Parser.Default.ParseArguments(args, options);
Console.WriteLine(options.sitename);
Console.WriteLine("\n");
Console.WriteLine(options.listname);
}
}
}
However, when I try calling this from CMD:
test -s sitename -l listname
I am getting this error:
Unhandled Exception: System.IO.FieNotFoundEsxcepion: Could not load file or assembly 'CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeytoken=de6f01bd326f8c32', or one of its dependancies. The system cannot find the file specified. at ConsoleApplication1.Program.Main(String[] args)
I have installed the CommandLineParser package and I can see it in my references list. When I navigate to this folder: \\file\IT\SK\Visual Studio Projects\ConsoleApplication1\packages\CommandLineParser.1.9.71\lib\net40 I can see that there is a CommanLine.dll and a CommandLine.xml.
Can someone please explain to me what is going on here?
Update
I am able to run this with command lines in Visual Studio if I DISABLE ClickOnce Security settings, and it works fine. However, when I publish the application, this is automatically selected and the problem persists.
When debugging with command line arguments and ClickOnce Security enabled, args[] is null ...
It's a security feature. Your executable resides on the network and is not trusted to access other network resources.
I bet it works if you copy everything to a local folder.
Is the dll also in the bin output folder? If not, check the properties of the reference and check that "copy local" is true.
If I feed Process.Start(); the parameters "Firefox", Notepad or "cmd" it runs those programs like their location is built in, but with other programs I have to specify the program's directory for it to work.
How does it automatically know where some programs located, and why only those programs and not others?
My code:
using System;
using System.Diagnostics;
namespace Testing
{
public class MainClass
{
static void Main()
{
Process.Start("Firefox"); // Works
Process.Start("Notepad"); // Works
Process.Start(#"C:\Users\user\Desktop\Steam"); // Works too
Process.Start("Steam"); // This line gives me "The System cannot find the file specified"(run-time error)
}
}
}
I think it depends on Environment variables in Windows.
or type PATH in cmd and observe paths, where *.exe files can be found automatically.
I am having problems with a application that wants to write to the .exe.config.
See following code:
using System;
using System.Configuration;
using System.IO;
namespace ConfigurationManagerError
{
class Program
{
static void Main(string[] args)
{
try
{
// set Config-File to persistent folder
ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap();
exeMap.ExeConfigFilename = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"ConfigError\\ConfigurationManagerError.exe.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add(
new KeyValueConfigurationElement("TestKey", "TestValue"));
config.Save(ConfigurationSaveMode.Modified);
Console.WriteLine("Successfully write of Configuration-File to " + config.FilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.Read();
}
}
}
As long as I run as a user with read and write access on the folder everything works.
If I have a user that has no write permission in the folder there is a exception that says that permission to write the .exe.config is not allowed.
Until now everything is as expected.
But if I now have a user that has the right to write to existing files but not to create new files an exception is thrown with something like
Access to the path 'C:\ProgramData\ConfigError\ila2ba_0.tmp' is denied
It seems that ConfigurationManager wants to create a tmp-File.
How can this be solved?
Thanks a lot!
Best regards,
Joerg
A user specific configuration setting which need to be rewritten multiple times should be saved under user's application data directory.
You can have a generic application configuration installed along with your application which can be merged with user specific configuration and required config setting can be overridden as per your requirement.
When I run a c# application through Visual Studio 2010, where R is integrated, I get the error: The program can't start because Rlapack.dll is missing from your computer. Try reinstalling the program to fix this problem.
I tried reinstalling the program but it did not work.
I also tried putting it in the folder that has the Matrix in it but it did not work. This solution was suggested in StackOverflow Q.
I am running 64-bit Windows 7! The application is 32-bit.
There are two dll's. One in a folder called i386, and another one in the folder x64.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string dlldir = #"D:\Program Files\R-2.15.0\bin\i386";
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(dlldir);
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch
{
MessageBox.Show(#"Unable to find R installation's \bin\i386 folder.
Press OK to attempt to locate it.");
}
}
}
}
}
I realize this has been answered, but that was back in 2012. For anyone still having this problem with R version 3.4.3 or later in 2018, especially while trying to follow the simple example from the r.net home page, below is what I did to fix it:
In your code, before the line REngine engine = REngine.GetInstance();, add this line REngine.SetEnvironmentVariables(#"C:\Program Files\R\R-3.4.3\bin\x64", #"C:\Program Files\R\R-3.4.3");.
right click project, go to build and uncheck "Prefer 32-bit".
copy Rlapack.dll from C:\Program Files\R\R-3.4.3\bin\i386
paste in both C:\Program Files\R\R-3.4.3\library\stats\libs\i386 and C:\Program Files\R\R-3.4.3\library\Matrix\libs\i386
copy Rlapack.dll from C:\Program Files\R\R-3.4.3\bin\x64
paste in both C:\Program Files\R\R-3.4.3\library\stats\libs\x64 and C:\Program Files\R\R-3.4.3\library\Matrix\libs\x64.
Such a pain, but that is what finally got it to work for me.
Here is what I just did and it worked: I put the dll in the bin folder of my application.
Try setting path variable before caling the dll:
var envPath = Environment.GetEnvironmentVariable("PATH");
string s = null;
if (Environment.Is64BitProcess)
s = #"C:\Program Files\R\R-2.15.0\bin\x64";
else
s = #"C:\Program Files\R\R-2.15.0\bin\i386";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + s);