Can not copy files to Environment.SpecialFolder.Startup in WPF - c#

To make my application to start on Windows sturup I decided to put a shortcut to Startup folder.
I tried to use:
File.Move(AppDomain.CurrentDomain.BaseDirectory + "ApplicationName.exe", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk");
It works, but it moves my shortcut not to the folder I need.
Environment.GetFolderPath(Environment.SpecialFolder.Startup)
works well, it returns:
C:\Users\Germanov\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
But my shortcut appears in
C:\Users\Germanov\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Just 1 folder "behind".
File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk");
also works "strange". It actually deletes this file, but again not in "Startup" folder.
If I try to manually add "\Startup" to the path like this:
Environment.GetFolderPath(Environment.SpecialFolder.Startup) + #"Startup\ApplicationName.lnk"
I get a System.IO.Excseption.
I can't type this path manually, I neen my application to work at diferent PCs with different versions of Windows. I also can't use Registry to make my application start with windows startup.
I use Windows 7, Visual Studio 2010, .NET 4.0, this is a WPF project.
Any ideas?

Did you tried Environment.SpecialFolder.CommonStartup instead of Startup, I don't know why startup is not working for your requirement. Most of installer package do this for you; why do you want to do this for your self? Any reason not for using Registry?
I tried this code on my machine
var startup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
string file = Path.Combine(startup, "MyApp.lnk");
using (StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Test");
}
And its coming on my startup

You should use System.IO.Path.Combine() so that you won't create StartupApplication1.exe. Note the missing backslash.

Related

.NET executables do not work after overwritten with new versions

I faced very strange behaviour - after I overwrite .NET exectables with new versions from network drive it cannot start.
When try to start from Windows Explorer it shows me following error:
[Window Title]
C:\Programs\zzz\clients.net\zzzNet.exe
[Content]
C:\Programs\zzz\clients.net\zzzNet.exe
The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.
I tried to execute following commands:
SxsTrace Trace -logfile:SxsTrace.etl
SxStrace Parse -logfile:SxSTrace.etl -outfile:SxSTrace.txt
And got following result:
=================
Begin Activation Context Generation.
Input Parameter:
Flags = 0
ProcessorArchitecture = AMD64
CultureFallBacks = en-US;en;ru-RU;ru
ManifestPath = C:\Programs\zzz\clients.net\zzzNet.exe
AssemblyDirectory = C:\Programs\zzz\clients.net\
Application Config File = C:\Programs\zzz\clients.net\zzzNet.exe.Config
-----------------
INFO: Parsing Application Config File C:\Programs\zzz\clients.net\zzzNet.exe.Config.
INFO: Parsing Manifest File C:\Programs\zzz\clients.net\zzzNet.exe.
INFO: Manifest Definition Identity is (null).
ERROR: Line 0: XML Syntax error.
ERROR: Activation Context generation failed.
End Activation Context Generation.
It is quite simple .NET application (1 exe + 8 dll files). It was built for .NET 3.5 Client Profile.
I not defined any special "manifest" there. Just clicked "New Windows Forms Project" in Visual Studio and developed it.
Also app.config does not contain anything special - only primitive standard settings - appSettings and userSettings sections.
On PC where I developed it all is perfectly works. Problems only began when I copy these binaries to this particular VM and try to start.
Also please note - these executables were not installed in GAC or such, I just copied them into a folder on VM and started. And when it was 1st time all was working fine.
So, the problem pattern is following:
Copy .NET execuatbles to new VM (it is Win 7 x64), run it, all is working fine. Close it.
Build new version of .NET execuatbles on host PC, copy new .NET execuatbles to VM (with files overwriting).
Try to start - got mentioned problem.
After some shaman-style actions (like OS reboot, etc) it begin to work but why that happened at all?!
Why replacing .NET executables with new versions is causing SUCH HUGE PROBLEMS?!
Also the BIG QUESTION - is there any special procedure to replace .NET executables to keep them working? Because it is a new app development, I do not want lost so much time on every new executables installation. :-\
Could you please help? Because it looks completely weird!
Thank you in adance.
PS. I checked all VS projects - all they have PlatformTarget=AnyCPU. Also in run-time I can see ProcessType=MSIL (I show this info in AboutBox for application). So, there is no mix of x86/x64 binaries.
It seems that was related to mapped network drive behavior.
When I copied new files from network drive folder it copied wrong files - a strange random mess of new files and older ones (which were there before I updated them on VM host).
The scenario to make it working:
on VM: delete all files in a folder on network drive
on VM host: copy new files into a folder which is mapped as network drive on VM
on VM: copy files into target folder
on VM: run application - it works now
Weird thing. I remember I have seen something similar with Windows Explorer on Windows 2008 behaviour when copying updated win32 binaries.

Cannot create files on Android with Xamarin

I have a Xamarin-Studio App for Android and I simply want to download files and save them locally. But when I try to create a file in the files folder I get an exception:
File.Create("data/data/com.company.app/files/newFile.png");
gives me:
System.UnauthorizedAccessException
Access to the path 'data/data/com.company.app/files/newFile.png' is denied.
What am I doing wrong?
You should use Environment or IsolatedStorage. For example:
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, "newFile.png");
I am coding Xamarin with VS2013. I had the access denied error for a directory created with the application I am writing. My application creates a directory called /storage/emulated/0/POIApp by concatenating via:
System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "POIApp");
I found that I had to use VS2013 to edit the "properties" of my application (POIApp), i.e., right-click the project icon in the solution explorer; choose properties from the pop-up menu. A new tab appears in the main VS2013 window. On the left there are a few choices, e.g., Application, Android Manifest, Android Options, Build, etc. Choose "Android Manifest". At the bottom of the main panel is a section "required permissions". My problem was solved when I checked "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE".
Add the following permission to Android.Manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I finally realized that File.create() was not the problem. I had code like this:
string tmpFilePath = FilesDir.AbsolutePath.stringByAppendingPath (f.Path);
Java.IO.File tmpFile = new Java.IO.File( tmpFilePath);
tmpFile.Mkdirs ();
Yet, Mkdirs() does not only create all intermediate directories – as I had assumed – but also creates a directory at the file path itself. So the file could not be created because there already was a directory with the same name.
The correct way is:
string tmpFile = FilesDir.AbsolutePath.stringByAppendingPath (f.Path);
Java.IO.File tmpParentFolder = new Java.IO.File(tmpFile).getParentFile();
tmpParentFolder.Mkdirs ();
In my defense, an FileExistsAndIsDirectory exception would have been much more helpful than the UnauthorizedAccessException
Using Mono, I think must be the same as in Xamarin Studio.
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
File.Create(path + "newFile.png");

Programmatically-created subfolder not visible in Windows Explorer

I'm trying to add a subdirectory to an existing path.
Win-7 64, Intel SSD
Logged into company domain as a user with admin priv, but not as "Administrator"
Visual Studio 2008, launched normally (not "Run as Admin")
Here's MyApp (WinForms, Debug, x86):
[STAThread]
private static void Main(string[] args)
{
string p = #"C:\ProgramData\MyCompany\MyApp";
Directory.CreateDirectory(p);
string f = "a.txt";
string fullPath = Path.Combine(p, f);
File.WriteAllText(fullPath, string.Empty);
Directory.Delete(p);
As expected, the last line throws IOException: "The directory is not empty". Should mean the subdir and file got created.
Problem: Windows Explorer does not show the MyApp subdir (even after refresh, type path in address bar, close/reopen, or reboot).
C:\ProgramData\MyCompany\ was created by another application's installer; Windows Explorer says its current owner is System.
Used same IDE to create a console app (Debug, x86), copied the above lines and ran; Windows Explorer was happy to show me DigitalTestApps and the file inside.
If I do any one of the following to MyApp, the problem goes away (i.e. Windows Explorer shows me the "MyApp" subdir and the file inside):
Build as x64
Launch Visual Studio using "Run as Admin"
Use any name other than "MyApp" (but I need to use MyApp for legacy reasons)
What could be causing this?
#dtb was too humble (or busy) to repost his comment (which solved my problem) as an answer, so I'll copy his comments here to make it easier for others to see how this turned out:
"Check if you can find your directory & file somewhere in C:\Users\JimC\AppData (Folder virtualization).
related: Why Virtualization on ProgramData folder in MS Vista?"
So here is what I did after reading his advice:
I deleted the virutalized directory from under C:\Users and it started working fine. Not sure how it got created in the first place, but seemingly, while it exists, attempts to create the directory where I was expecting it finds the virtualized one instead.
Thanks again dtb.

How to get installation path of an application?

In Windows using C#, how can I get the installation path of a software (for example consider NUnit or any other software like MS word, etc.) from my project? Also how to set the path variables that we set in Environment variables so that we can run the application just by giving in command prompt.
Like if I install NUnit in "C:\Program Files" I can run it by giving 'NUnit' in cmd prompt but if I install in a different location I can't do the same.
I need to get the location or path of NUnit or any other software installed in my system (having Windows XP) from my project.
EDIT:
Like I can get the path of installed program from registry.
HKEY_CURRENT_USER->SOFTWARE
Use the system and application classes. This will give you all sorts of information.
EG: Application.ExecutablePath
It also provides methods to do what you want to.
Edit: Also see registry read/write instructions here:
http://www.c-sharpcorner.com/UploadFile/sushmita_kumari/RegistryKeys102082006061720AM/RegistryKeys1.aspx?ArticleID=0ce07333-c9ab-4a6a-bc5d-44ea2523e232
Application.ExecutablePath (includes filename)
Application.StartupPath (not includes filename)
This will give you the path where the application started. Hopefully it will be the installation path.
string appFileName = Environment.GetCommandLineArgs()[0];
will give you the full path of the executable and
string directory = Path.GetDirectoryName(appFileName);
extracts the directory.
string envPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable(envPath + ";" + yourPath);
edits the PATH environment variable for the current process.
Application.StartupPath is used to get installation location in c#.
Like if i install Nunit in "C:\Program
Files" i can run it by giving 'nunit'
in cmd prompt but if i install in a
different location i cant do the same.
May be you are using Windows Vista, which can search in Program Files, but won't look in other folders.
In windows using C#, how to get the
installation path of a software(for
example consider nunit).?
It depends, how you are installing the application. The installer knows the path, you may program the installer to write that path to somewhere, say registry.
Also how to set the path variables
that we set in Environment variables
so that we can run the application
just by giving in command prompt.
How do I get and set Environment variables in C#?
Steps to extract value from registry are shown in following code snippet.
You may already know that there are no standard rules for applications to place their installation info.
The steps shown below are for COM based applications where the appplication must provide Local executable path in a reasonably standard manner.
For non-com applications, check to see if some data can be extracted from installed applications cache.
I hate to admit that the solution is not as elegant as I want it to be. Each subkey has to opened in series and opening in single method does not work.
//string hiveName = #"CLSID"; // for 64 bit COM 7applications
string hiveName = #"WOW6432Node\CLSID"; // for 32 bit COM applications
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(hiveName))
{
if (key != null) {
using (RegistryKey key2 = key.OpenSubKey("{<YourAppGUID>}"))
{
if (key2 != null) {
using (RegistryKey key3 = key2.OpenSubKey("LocalServer32"))
{
if (key3 != null) {
return key3.GetValue("").ToString();
}
}

The filename, directory name, or volume label syntax is incorrect, c#

i've written a console application deploy.exe which runs a batch script.
Process p1 = new Process();
p1.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "installer.bat";
p1.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p1.Start();
p1.WaitForExit();
p1.Close();
the installer.bat conatins the following command.
\shared1\lists\list1.cmd
If i run the executable byitself it runs successfully.
However i needed it to run in a windows installer project. So i made a setup and deployment project and added the deploy.exe successfully as custom action upon install.
It runs fine but when it starts to execute the command i get this error
"The filename, directory name, or volume label syntax is incorrect".
any help?
Try printing out what the value of AppDomain.CurrentDomain.BaseDirectory is. It may not be where installer.bat is when you are installing it.
Also, you tried adding the bat file to a custom action (if that is even possible)?
And, would it possible to move what is in the bat to the exe?
Is it a problem in your batch file?
Check this:
\\shared1\\lists\\list1.cmd
should probably be
\\shared1\lists\list1.cmd
Note the extra \ chars in your original command. That would cause the batch file to give that error.
the error seems to be inside the script which was being executed. It contained environment variables %kind%, which were not acceptable by the installer for some reason. So it was working properly outside the installer and not properly when the installer was calling it.

Categories