4 Replies - 3548 Views - Last Post: 06 March 2012 - 10:57 PM

#1 cps13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-December 11

Copying directory and moving with variables/action on click winformapp

Posted 27 December 2011 - 06:27 PM

My first problem is having a specific action occur when a button is pressed in a windows form application, such as:

void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->DialogResult = System::Windows::Forms::DialogResult::OK;
			this->button1->Location = System::Drawing::Point(91, 84);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(91, 22);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Make dir";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Server Backup";
			this->ResumeLayout(false);

		}
		


The specific line is this:

this->button1->DialogResult = System::Windows::Forms::DialogResult::OK;


I later have:

int funct(){
		if (DialogResult == System::Windows::Forms::DialogResult::OK)
	{
	mkdir("C:\\test\\TestDIR");
	}
	return 0;
	}



However, the directory is never made when the button is pressed.


My next issue, is in a command line program, I have:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <CString>
#include <direct.h>
#include <string>
#include <time.h>
using namespace std;
bool b = true;
int a;
string path = "c:\\Users\\Admin\\Desktop\\Server\\Test";







int main() {

while(b == true) {
	
	cin >>a;


	if (a == 0) {
    mkdir("path");
	system("xcopy C:\\Users\\Admin\\Desktop\\Servertest c:\\Users\\Admin\\Desktop\\Server\\Test /Y /I /E");
	cout <<GetLastError();

	}


}
}



The issue is my xcopy, I want to have a variable in the path.

For example, it uses the string path in the xcopy function instead of the entire c:\\users etc,

Also, A user input after the string.

So for example, you'd have string path = "c:\\Users\\Admin\\Desktop\\Server\\Test\\ & $test"

Where test is another string that the user inputs, like

cin >>test; or however.

Can anyone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Copying directory and moving with variables/action on click winformapp

#2 cps13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-December 11

Re: Copying directory and moving with variables/action on click winformapp

Posted 27 December 2011 - 06:36 PM

View Postcps13, on 27 December 2011 - 06:27 PM, said:

My first problem is having a specific action occur when a button is pressed in a windows form application, such as:

void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->DialogResult = System::Windows::Forms::DialogResult::OK;
			this->button1->Location = System::Drawing::Point(91, 84);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(91, 22);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Make dir";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Server Backup";
			this->ResumeLayout(false);

		}
		


The specific line is this:

this->button1->DialogResult = System::Windows::Forms::DialogResult::OK;


I later have:

int funct(){
		if (DialogResult == System::Windows::Forms::DialogResult::OK)
	{
	mkdir("C:\\test\\TestDIR");
	}
	return 0;
	}



However, the directory is never made when the button is pressed.


My next issue, is in a command line program, I have:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <CString>
#include <direct.h>
#include <string>
#include <time.h>
using namespace std;
bool b = true;
int a;
string path = "c:\\Users\\Admin\\Desktop\\Server\\Test";







int main() {

while(b == true) {
	
	cin >>a;


	if (a == 0) {
    mkdir("path");
	system("xcopy C:\\Users\\Admin\\Desktop\\Servertest c:\\Users\\Admin\\Desktop\\Server\\Test /Y /I /E");
	cout <<GetLastError();

	}


}
}



The issue is my xcopy, I want to have a variable in the path.

For example, it uses the string path in the xcopy function instead of the entire c:\\users etc,

Also, A user input after the string.

So for example, you'd have string path = "c:\\Users\\Admin\\Desktop\\Server\\Test\\ & $test"

Where test is another string that the user inputs, like

cin >>test; or however.

Can anyone help me?



____________

SORRY i'd like to make it clear I did system("xcopy etc

not xcopy("blah
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Copying directory and moving with variables/action on click winformapp

Posted 27 December 2011 - 06:53 PM

Moved to CLI C++
Was This Post Helpful? 0
  • +
  • -

#4 Supps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-February 12

Re: Copying directory and moving with variables/action on click winformapp

Posted 16 February 2012 - 06:39 AM

Dude you can't mix managed code with unmanaged code. To create a new directory use this code:

System::IO::Directory::CreateDirectory("C:\\MyNewDirectory");

Was This Post Helpful? 0
  • +
  • -

#5 ModMaker  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-July 10

Re: Copying directory and moving with variables/action on click winformapp

Posted 06 March 2012 - 10:57 PM

In case you are still looking for a solution, here is my two cents. You can still do this using unmanaged code if you want to.

First,

Quote

Dude you can't mix managed code with unmanaged code. To create a new directory use this code:

The point of c++/cli is to mix both managed and unmanaged code, you just have to be careful.

Second, the function is never called by the button because you do not register the function to the button. You need to call the function inside Form1::button1_Click, or add one of the following lines:

/* if funct is an instance method */
this->button1->Click += gcnew System::EventHandler(this, &Form1::funct);
/* if funct is either static or declared outside a class */
this->button1->Click += gcnew System::EventHandler(nullptr, &funct);


Third, passing a variable to system() is not possible with the following code because when using c++/cli, string constants are default System::String, which is not compatible with system() which accepts const char*.

system("my string" + var);


The way to do this is to convert the System::String to a const char*. There are two ways to do this, first is with an instance of msclr::interop::marshal_context, found in <msclr/marshal.h>. The other way is to use System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(string). Here is how to do it both ways:

#include<msclr/marshal.h>
msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context();
const char* asChars(System::String^ s)
{
	return context->marshal_as<const char*>(str);
}

const char* asChars(System::String^ s)
{
     return reinterpret_cast<const char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str).ToPointer());
}


The second way may look worse as it uses the taboo reinterpret_cast. The first way requires that you keep a reference to the instance of marshal_context, as when it is GC'd, the pointers that it creates are free'd. This is better, as long as you keep the reference. This is why it is not valid to have the variable defined in the function. When using the second way, you need to free the pointer using System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr).

This post has been edited by ModMaker: 06 March 2012 - 11:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1