I need little help on creating shortcut for my C# app. I want programmatically do this.
I intend to create a shortcut in startup menu folder. Maybe you could also show me how to start my app automatically using a registry entry. Thanks!

Dani AI

Generated

— that ambiguous File error comes from two types with the same short name being in scope. Instead of removing using System.IO; (as suggested) or always writing the full name, create a file-type alias so your call sites stay clean. Example (put at the top of the source file):

using FileIO = System.IO.File;

Then use FileIO.AppendAllText(...) in your code. This keeps readability and avoids name collisions. (learn.microsoft.com)

To place a shortcut in the right Startup folder, resolve the folder at runtime instead of hard-coding paths. Use Environment.GetFolderPath(...) with SpecialFolder.CommonStartup (all users) or SpecialFolder.Startup (current user), for example:

string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);

That avoids brittle, OS-specific paths. (learn.microsoft.com)

If you prefer a registry autostart entry, write into the Run key for the appropriate hive. Use HKCU for per-user entries and HKLM for machine-wide (HKLM needs elevation). Example pattern:

using Microsoft.Win32;

var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
key.SetValue("MyAppName", "\"" + Application.ExecutablePath + "\"");

Mind UAC and always check for existing values before overwriting. (learn.microsoft.com)

For creating the .lnk itself from managed code you can continue using the Windows Script Host WshShell.CreateShortcut (the IWshRuntimeLibrary model), or use a proper installer (MSI/Inno) to create shortcuts and Run entries at install time — installers handle permissions and rollback cleanly. If you keep the COM route, release COM objects and wrap file/registry writes in try/catch to handle permission failures gracefully. (devguru.com)

Recommended Answers

All 7 Replies

And what have you found on google so far?

Thanks for reply LizR.
Of course I googled that, and I had a little problem I'll tell you now. I made a reference to "Windows Script Host Object Model", then I included IWshRuntimeLibrary and I made a button for testing, so that when I click it, process for creating the shortcut starts, like this:

private WshShellClass WshShell;

private void button3_Click(object sender, EventArgs e)
        {
            // Create a new instance of WshShellClass

            WshShell = new WshShellClass();

            // Create the shortcut

            IWshRuntimeLibrary.IWshShortcut MyShortcut;

            // Choose the path for the shortcut

            MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(@"C:\Documents and Settings\All Users\Desktop\cLchecker.lnk");

            // Where the shortcut should point to

            MyShortcut.TargetPath = Application.ExecutablePath;

            // Description for the shortcut

            MyShortcut.Description = "Launch cLchecker";

            // Location for the shortcut's icon

            MyShortcut.IconLocation = Application.StartupPath + @"\cLchecker.ico";

            // Create the shortcut at the given path

            MyShortcut.Save();
        }

The problem is that I use File.AppendAllText() method to write to file and I got this error:
" 'File' is an ambiguous reference between 'System.IO.File' and 'IWshRuntimeLibrary.File' ".

It seems to be a little conflict. So, don't worry, I don't wanna lose your time with nothing; I told you now the main problem in my program. And sorry for not pointing to the exact problem. I'll go now; in 7 hours I'm back.

use something like : System.IO.File.AppendAllText

It doesn't work. However thanks for reply.
Somebody else? Ideas?

You dont show the code you're using so we're only guessing..

Your short cut looks like it should appear after your code above.. Whats the file bit for? something else?

Try using System.IO.File.Something or IWshRuntimeLibrary.File.Something.

what you have to do is remove "using System.IO;" from the top of your class, and everytime you need to use System.IO.File you will have to type the full namespace. problem is that when you import the namespace for the object that creates your shortcut you share its namespace scope with the "File" namespace scope of the System.IO, so removing the using block at the top of your class fixes the problem.

Happy Coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.