I need to delete a shortcut but the code I wrote will not work.

System.IO.File.Delete("C:/WINDOWS/Example.Ink"); // This returns no errors at all, just nothing happens.

Does anyone have an idea on an alternate way to delete a shortcut?

Any help is appreciated =D

It works with me, do you work on Vista? if yes you run VS\your application as administrator.

I've tried this on multiple computers (All WIN XP SP3) and it still doesn't work. Any other suggestions?

Just to be sure : is your file named Example.Ink or Example.lnk ?

Yay, that's it. But it is a pretty embarrassing mistake considering I made it before while I was building the application to create the shortcuts. :icon_cheesygrin:

Thanks heaps for the help!

There are no embarrassing mistakes!
You would not want to know how many mistakes I made!
But it is from your mistakes that you learn.
Mark this thread as solved please and happy computing;)

Yes, it doesn't throw any exception if file not exists and they documented it.
Deletes the specified file. An exception is not thrown if the specified file does not exist.

public static void Delete(string path)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    string fullPathInternal = Path.GetFullPathInternal(path);
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { fullPathInternal }, false, false).Demand();
    if (Environment.IsWin9X() && Directory.InternalExists(fullPathInternal))
    {
        throw new UnauthorizedAccessException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), new object[] { path }));
    }
    if (!Win32Native.DeleteFile(fullPathInternal))
    {
        int errorCode = Marshal.GetLastWin32Error();
        if (errorCode != 2)
        {
            __Error.WinIOError(errorCode, fullPathInternal);
        }
    }
}

As you see it just checks if path is null or not and the permission.

Some exceptions too
Exception Condition
IOException The specified file is in use.
ArgumentNullException path is null.
DirectoryNotFoundException The specified path is invalid, (for example, it is on an unmapped drive).
NotSupportedException path is in an invalid format.
UnauthorizedAccessException The caller does not have the required permission.-or- path is a directory.-or- path specified a read-only file.
ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

I expect they assume developer will write delete file code like this

public void DeleteFile(string filePath)
{
if(File.Exists(filePath))
File.Delete(filePath);
else
MessageBox.Show("File not exists, deleting file not performed");
//or throw an error message in their face
}

Did you try?

System.IO.File.Delete("C:\\WINDOWS\\Example.Ink"); // This should work.

I need to delete a shortcut but the code I wrote will not work.

System.IO.File.Delete("C:/WINDOWS/Example.Ink"); // This returns no errors at all, just nothing happens.

Does anyone have an idea on an alternate way to delete a shortcut?

Any help is appreciated =D

it is not his fault though, this thread must have been marked as solved long ago. That is normal that people keep opening and reading it and suggesting similar solutions over and over again.

We should flag a thread as bad one, and report the asker didn't react with us and mark the thread as solved, so they do it.
Opinion?

i think one of us or some of us has to have a right to mark threads as solved like in msdn forums. i post there questions and they are marked as solved after a while although i didnt mark them my self.
i asked to dani and happy geek for that right to be granted to me but they just ignored me. Because of Narue i am not taken seriously in daniweb :(

ask ddanbe first, this time i am not going to open the thread since probably jphtaph or Wiliam or Mosaic Funeral will attach me. Actually it will be more proper if ddanbe starts a thread like that.

You mean "attack" you not "attach" you.
Then what's bad in me to start a thread, Am not I getting people respect?!

This article has been dead for over six months. Start a new discussion instead.