반응형

폴더 생성 후 권한없다고 뜰때 사용

private void CreateResourceDirectory()
{
	DirectorySecurity directorySecurity = new DirectorySecurity(); 
	directorySecurity.SetAccessRuleProtection(true, false); 
	SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

	// 디렉토리에 모든 사용자가 사용할 수 있게 권한 주기
	directorySecurity.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Write | 
	FileSystemRights.ReadAndExecute | FileSystemRights.WriteAttributes 
	| FileSystemRights.Delete | FileSystemRights.CreateFiles, InheritanceFlags.ObjectInherit 
	| InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); 

	DirectoryInfo difo = new DirectoryInfo(DirectoryPath); // 원하는 위치에 객체생성
	if (difo.Exists == false) // 폴더의 존재유무 확인
	{ 
   		difo.Create(directorySecurity); // 권한추가한 폴더 생성
   		difo.Attributes = FileAttributes.Hidden; // 숨김 폴더로 만들기
	}
}

private void DeleteResourceDirectory()
{
    try // 권한을 줘도 삭제오류 발생시 오류 잡기
    {
    	DirectoryInfo fifo = new DirectoryInfo(DirectoryPath);
    	fifo.Delete(true);
    }
    catch (Exception ex)
    {
    	MessageBox.show(ex.Message);
    }
}
반응형
반응형
public string PostData(string url, string postData) //Http Post방식 
{ 
	try 
	{ 
		WebClient webClient = new WebClient(); 
        	//JSON 타입도 application/x-www-form-urlencoded 타입으로 받아진다.
		webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; 
		webClient.Encoding = UTF8Encoding.UTF8; 
		string responseData = webClient.UploadString(url, postData);   //매개변수로 받은 URL에 데이터 송수신 
		webClient.Dispose(); 
		return responseData; // 받은 문자열 데이터를 split으로 나눠서 JSON파싱을 직접할 수 있다.
	} 
	catch (Exception ex) 
	{ 
		CLogMessage(ex.Message); 
		return ""; 
	} 
}

 

반응형
반응형

https://colorscripter.com/

반응형
반응형
//폼디자인 도구에서 progressBar 추가
using System.Net;

WebClient myWebClient = new WebClient();

// 이벤트 핸들러 선언
// 업로드 중일때 이벤트
myWebClient.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);

// 업로드가 완료되었을때 이벤트
myWebClient.UploadFileCompleted += new UploadFileCompletedEventHandler(ploadFileCompleted);

//업로드 중일때 콘솔에 몇 바이트씩 다운로드 받는지 찍기
void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
	Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...",
	(string)e.UserState, e.BytesSent, e.TotalBytesToSend, Math.Truncate
    (((double)e.BytesSent / (double)e.TotalBytesToSend) * 100));
    
	progressBar1.Value = int.Parse(Math.Truncate
    (((double)e.BytesSent / (double)e.TotalBytesToSend) * 100).ToString());

}

//업로드가 완료되었을 때 이벤트핸들러
void ploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
	try
	{
		string Result = Encoding.UTF8.GetString(e.Result); //파일 업로드 완료후 리턴된 string
		Console.WriteLine("Return Value : " + Result);
	}
	catch (Exception ex)
	{
		Console.WriteLine("Return Execption : " + ex);
	}
}
반응형

+ Recent posts