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

Recommended Answers

All 24 Replies

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

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 ?

commented: good catch +4

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;)

i am surprised that it didnt generate any errors though, it must have thrown filenotfoundexception.

commented: Nice remark! +7

serkan sendur,
Delete() method cannot throws an exception.

commented: I did not know that, thanks! +7

shame on that method then :)

serkan and adatapost.
Thanks for the remarks on Delete!

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 take the shame back then :)

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

JuhaW, wake up and let's go to fishing :)

JuhaW, after you have gone fishing with Serkan you could perhaps read post #4 of this thread?

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 :(

Let's open a thread on Community Feedback not on Geek lounge to discuss such an issue.

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?!

you start then, i just suggested ddanbe as he looks older :)

commented: Other that I promised you, your reply.. I liked, Danny is old and respected man +9

To stop the discussion who should start what in where:
Here's what you're looking for and why it hasn't been implemented.

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.