DeleteFile(String^.c_str()); doesn't work.

Hello,

I am trying to have a program delete a file using the DeleteFile function passing a string argument with a .c_str() conversion, but VS2010 keeps throwing a "error C2228: left of '.c_str' must have class/struct/union" error.

remove(String^.c_str()) gives the same error as well.

I've included sstream, and windows.h.

I can't seem to find an answer via Google, since nobody else seems to have this issue.

Anybody know what could be the issue here?
You're not using C++. The portion to the left of the dot should be a variable, not a type.
Just to expand on what cire is saying:
http://en.wikipedia.org/wiki/C%2B%2B/CLI
If you're working with C++/CLI you need the .NET equivalent of the WinAPI DeleteFile() function: File::Delete()

Assuming you project has included the required assemblies, then it's:

...

1
2
using namespace System;
using namespace System::IO;


...

1
2
    System::String^ fileName = "test.txt";
    File::Delete(fileName);


Andy

PS You could convert the System::String to a std::string and then use it with either DeleteFile() or remove(), but that's not really the way to go in C++/CLI code.

If you know your way around WinAPI calls and the C and C++ standard libraries, but not .NET, then google e.g. ".NET equivalent of DeleteFile" -- it gives me the page "File.Delete Method (System.IO) - MSDN - Microsoft" as the first hit. Note that a lot of the examples are in C# but it's pretty easy to convert basic C# into C++/CLI
Last edited on
I need to remember that using C++/CLI.

File::Delete worked, thanks.
Topic archived. No new replies allowed.