https://docs.microsoft.com/ko-kr/visualstudio/deployment/walkthrough-downloading-assemblies-on-demand-with-the-clickonce-deployment-api?view=vs-2022 

 

주문형 어셈블리 다운로드(ClickOnce API) - Visual Studio (Windows)

ClickOnce 애플리케이션의 특정 어셈블리를 선택 사항으로 표시하고 공용 언어 런타임에 필요할 때 다운로드하는 방법을 알아봅니다.

docs.microsoft.com

 

 

 

연습: ClickOnce 배포 API를 통해 주문형 어셈블리 다운로드

 

기본적으로 ClickOnce 애플리케이션에 포함된 모든 어셈블리는 애플리케이션을 처음 실행할 때 다운로드됩니다. 그러나 소수의 사용자가 사용하는 일부 애플리케이션이 있을 수 있습니다. 이 경우 해당 형식 중 하나를 만들 때에만 어셈블리를 다운로드하고자 할 수 있습니다. 다음 연습에서는 애플리케이션의 특정 어셈블리를 "선택 사항"으로 표시하는 방법 및 CLR(공용 언어 런타임)에서 요청할 때 System.Deployment.Application 네임스페이스에 있는 클래스를 사용하여 이를 다운로드하는 방법을 설명합니다.

 참고

이 절차를 사용하려면 완전 신뢰 상태에서 애플리케이션을 실행해야 합니다.

사전 요구 사항

이 연습을 완료하려면 다음 구성 요소 중 하나가 필요합니다.

  • Windows SDK. Windows SDK는 Microsoft 다운로드 센터에서 다운로드할 수 있습니다.
  • Visual Studio.

프로젝트 만들기

주문형 어셈블리를 사용하는 프로젝트를 만들려면

  1. ClickOnceOnDemand라는 디렉터리를 만듭니다.
  2. Windows SDK 명령 프롬프트 또는 Visual Studio 명령 프롬프트를 엽니다.
  3. ClickOnceOnDemand 디렉터리로 변경합니다.
  4. 다음 명령을 사용하여 퍼블릭/프라이빗 키 쌍을 생성합니다.
    sn -k TestKey.snk
    
  5. cmd복사
  6. 메모장 또는 다른 텍스트 편집기를 사용하여 Message라는 단일 속성을 사용하여 DynamicClass라는 클래스를 정의합니다.
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Microsoft.Samples.ClickOnceOnDemand
    {
        public class DynamicClass
        {
            public DynamicClass() {}
    
            public string Message
            {
                get
                {
                    return ("Hello, world!");
                }
            }
        }
    }
    
  7. C#복사
  8. 사용하는 언어에 따라 ClickOnceLibrary.cs 또는 ClickOnceLibrary.vb 라는 파일로 텍스트를 ClickOnceOnDemand 디렉터리에 저장합니다.
  9. 파일을 어셈블리로 컴파일합니다.
    csc /target:library /keyfile:TestKey.snk ClickOnceLibrary.cs
    
  10. C#복사
  11. 어셈블리를 위한 퍼블릭 키 토큰을 얻으려면 다음 명령을 사용합니다.
    sn -T ClickOnceLibrary.dll
    
  12. cmd복사
  13. 텍스트 편집기를 사용하여 새 파일을 만들고 다음 코드를 입력합니다. 이 코드는 필요한 경우 ClickOnceLibrary 어셈블리를 다운로드하는 Windows Forms 애플리케이션을 만듭니다.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    using System.Deployment.Application;
    using Microsoft.Samples.ClickOnceOnDemand;
    
    namespace ClickOnceOnDemand
    {
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Unrestricted=true)]
        public class Form1 : Form
        {
            // Maintain a dictionary mapping DLL names to download file groups. This is trivial for this sample,
            // but will be important in real-world applications where a feature is spread across multiple DLLs,
            // and you want to download all DLLs for that feature in one shot. 
            Dictionary<String, String> DllMapping = new Dictionary<String, String>();
    
            public static void Main()
            {
                Form1 NewForm = new Form1();
                Application.Run(NewForm);
            }
    
            public Form1()
            {
                // Configure form. 
                this.Size = new Size(500, 200);
                Button getAssemblyButton = new Button();
                getAssemblyButton.Size = new Size(130, getAssemblyButton.Size.Height);
                getAssemblyButton.Text = "Test Assembly";
                getAssemblyButton.Location = new Point(50, 50);
                this.Controls.Add(getAssemblyButton);
                getAssemblyButton.Click += new EventHandler(getAssemblyButton_Click);
    
                DllMapping["ClickOnceLibrary"] = "ClickOnceLibrary";
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            }
    
            /*
             * Use ClickOnce APIs to download the assembly on demand.
             */
            private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                Assembly newAssembly = null;
    
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;
    
                    // Get the DLL name from the Name argument.
                    string[] nameParts = args.Name.Split(',');
                    string dllName = nameParts[0];
                    string downloadGroupName = DllMapping[dllName];
    
                    try
                    {
                        deploy.DownloadFileGroup(downloadGroupName);
                    }
                    catch (DeploymentException de)
                    {
                        MessageBox.Show("Downloading file group failed. Group name: " + downloadGroupName + "; DLL name: " + args.Name);
                        throw (de);
                    }
    
                    // Load the assembly.
                    // Assembly.Load() doesn't work here, as the previous failure to load the assembly
                    // is cached by the CLR. LoadFrom() is not recommended. Use LoadFile() instead.
                    try
                    {
                        newAssembly = Assembly.LoadFile(Application.StartupPath + @"\" + dllName + ".dll," +  
                "Version=1.0.0.0, Culture=en, PublicKeyToken=03689116d3a4ae33");
                    }
                    catch (Exception e)
                    {
                        throw (e);
                    }
                }
                else
                {
                    //Major error - not running under ClickOnce, but missing assembly. Don't know how to recover.
                    throw (new Exception("Cannot load assemblies dynamically - application is not deployed using ClickOnce."));
                }
    
    
                return (newAssembly);
            }
    
            private void getAssemblyButton_Click(object sender, EventArgs e)
            {
                DynamicClass dc = new DynamicClass();
                MessageBox.Show("Message: " + dc.Message);
            }
        }
    }
    
  14. C#복사
  15. 코드에서 LoadFile에 대한 호출을 찾습니다.
  16. PublicKeyToken을 이전에 검색한 값으로 설정합니다.
  17. 파일을 Form1.cs 또는 Form1.vb 로 저장합니다.
  18. 다음 명령을 사용하여 실행 파일로 컴파일합니다.
    csc /target:exe /reference:ClickOnceLibrary.dll Form1.cs
    
  19. C#복사

어셈블리를 선택 사항으로 표시

MageUI.exe를 사용하여 ClickOnce 애플리케이션에서 어셈블리를 선택 사항으로 표시하려면

  1. MageUI.exe 를 사용하여 연습: ClickOnce 애플리케이션 수동으로 배포에 설명된 대로 애플리케이션 매니페스트를 만듭니다. 애플리케이션 매니페스트에 대해 다음 설정을 사용합니다.
    • 애플리케이션 매니페스트의 이름을 ClickOnceOnDemand 로 지정합니다.
    • 파일 페이지의 ClickOnceLibrary.dll 행에서 파일 형식 열을 없음 으로 설정합니다.
    • 파일 페이지의 ClickOnceLibrary.dll 행에서 Group 열에 ClickOnceLibrary.dll을 입력합니다.
  2. MageUI.exe 를 사용하여 연습: ClickOnce 애플리케이션 수동으로 배포에 설명된 대로 배포 매니페스트를 만듭니다. 배포 매니페스트에 대해 다음 설정을 사용합니다.
    • 배포 매니페스트의 이름을 ClickOnceOnDemand로 지정합니다.

새 어셈블리 테스트

요청 시 어셈블리를 테스트하려면

  1. 웹 서버에 ClickOnce 배포를 업로드합니다.
  2. 배포 매니페스트에 URL을 입력하여 웹 브라우저에서 ClickOnce로 배포된 애플리케이션을 시작합니다. ClickOnceClickOnceOnDemand 애플리케이션을 호출하고 adatum.com 루트 디렉터리에 업로드하면 URL은 다음과 같습니다.
    http://www.adatum.com/ClickOnceOnDemand/ClickOnceOnDemand.application
    
  3. 복사
  4. 기본 폼이 나타나면 Button을 누릅니다. 메시지 상자 창에 "Hello, World!"라는 문자열이 표시되어야 합니다.

참조


권장 콘텐츠

  • ClickOnce 애플리케이션 - Visual Studio (Windows)

    게시 마법사를 사용하여 처음으로 ClickOnce 애플리케이션을 게시하는 방법을 알아봅니다. Project 디자이너의 게시 페이지에서 나중에 변경합니다.

  • ClickOnce 앱 수동 배포 - Visual Studio (Windows)

    명령줄 버전 또는 매니페스트 생성 및 편집 도구의 그래픽 버전 중 하나를 사용 하 여 ClickOnce 배포를 만드는 방법에 대해 알아봅니다.

  • 로컬 & 원격 데이터 액세스(ClickOnce 앱) - Visual Studio (Windows)

    로컬 및 원격으로 데이터를 읽고 쓰기 위해 ClickOnce 제공하는 다양한 옵션에 대해 알아봅니다.

  • ClickOnce 및 응용 프로그램 설정 - Visual Studio (Windows)

    응용 프로그램 설정 파일이 ClickOnce 응용 프로그램에서 작동 하는 방법 및 사용자가 다음 버전으로 업그레이드 하는 경우 설정 ClickOnce 마이그레이션하는 방법에 대해 알아봅니다.

  • ClickOnce 업데이트 전략 선택 - Visual Studio (Windows)

    ClickOnce 애플리케이션에서 자동 업데이트를 지원하는 방법과 사용할 수 있는 업데이트 전략을 알아봅니다.

  • ClickOnce 응용 프로그램에 대 한 코드 액세스 보안 - Visual Studio (Windows)

    ClickOnce 응용 프로그램에 대 한 코드 액세스 보안 및 코드 액세스 보안 권한을 구성 하는 방법에 대해 알아봅니다.

  • ClickOnce 애플리케이션 보안 - Visual Studio (Windows)

    ClickOnce 응용 프로그램에 대 한 코드에 대 한 액세스를 제한할 수 있는 .NET Framework의 코드 액세스 보안 제약 조건에 대 한 영향에 대해 알아봅니다.

  • ApplicationDeployment 클래스 (System.Deployment.Application)

    현재 배포를 프로그래밍 방식으로 업데이트하도록 지원하고 요청 시에 파일을 다운로드하도록 처리합니다.Supports updates of the current deployment programmatically, and handles on-demand downloading of files. 이 클래스는 상속될 수 없습니다.This class cannot be inherited.

 

Version CurrentVersion
{
	get
	{
		if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
		{
			System.Deployment.Application.ApplicationDeployment ad = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
			return ad.CurrentVersion;
		}
		else
		{
        	//string fileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
        	//Assembly.LoadFile(fileName).GetName().Version;
			return Assembly.GetExecutingAssembly().GetName().Version;
		}
	}
}

Version UpdateVersion
{
	get
	{
		if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
		{
			System.Deployment.Application.ApplicationDeployment ad = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
			return ad.UpdatedVersion;
		}
		return null;
	}
}

 

using Newtonsoft.Json.Linq;
//======
Json Write
//======
void WriteJson(){
	JObject jProp = new JObject
	{
		{ "id", "user" },
		{ "name", "Pillar Park" },
	};
	jProp.Add("email", "userpark@userpark.net");
	jProp.Add("country", "Korea (South)");

	JObject jRoot = new JObject(new JProperty("Root", jAttr));

	File.WriteAllText("test.json", jRoot.ToString());
}


//======
Json Read
//======
void ReadJson()
{
	string str = null;
	using (StreamReader sr = new System.IO.StreamReader("test.json"))
	{
		str = sr.ReadToEnd();
		sr.Close();
	}
    //str = File.ReadAllText("test.json");
    
    JObject jRoot = JObject.Parse(str);
    JToken jToken = jRoot["Root"];
    if (jToken["id"] != null)
    {
    	Console.WriteLine(jToken["id"].ToString());
    }
}

 

/**
// KeyDown : 컨트롤에 포커스가 있을 때 키를 누르면 발생합니다.
//   - https://docs.microsoft.com/ko-kr/dotnet/api/system.windows.forms.control.keydown?view=net-5.0
// KeyPress : 컨트롤에 포커스가 있을 때 문자, 스페이스 또는 백스페이스 키를 누르면 발생합니다.
//   - https://docs.microsoft.com/ko-kr/dotnet/api/system.windows.forms.control.keypress?view=net-5.0
// KeyUp : 컨트롤에 포커스가 있을 때 키를 눌렀다 놓으면 발생합니다.
//   - https://docs.microsoft.com/ko-kr/dotnet/api/system.windows.forms.control.keyup?view=net-5.0
 */

//===========================================================

edtSampleControl.KeyDown += OnKeyDown_Enter; //엔터값 확인 
private void OnKeyDown_Enter(object sender, KeyEventArgs e)
{

	Console.WriteLine($"KeyDown code: {e.KeyCode}, value: {e.KeyValue}, modifiers: {e.Modifiers}" + "\r\n");
    
	if (e.KeyCode == Keys.Enter)
	{
		//엔터입력 처리
		e.Handled = true;
	}
}

//===========================================================

edtSampleControl.KeyPress += OnKeyPress_OnlyNumberic; //숫자, 백스페이스, 삭제만 허용
private void OnKeyPress_OnlyNumberic(object sender, KeyPressEventArgs e)
{
	Console.WriteLine($"KeyPress keychar: {e.KeyChar}" + "\r\n");

	//숫자, 백스페이스, 삭제만 허용
	char keyChar = e.KeyChar;
	if (!(char.IsDigit(keyChar) || e.KeyChar == Convert.ToChar(Keys.Back) || e.KeyChar == Convert.ToChar(Keys.Delete)))    //숫자와 백스페이스, 삭제를 제외한 나머지를 바로 처리
	{
		e.Handled = true;
	}
}

//===========================================================

edtSampleControl.KeyPress += OnKeyPress_EscapeToClear; //Escape(ESC) 입력시 Text지우기
private void OnKeyPress_OnlyNumberic(object sender, KeyPressEventArgs e)
{

	Console.WriteLine($"KeyPress keychar: {e.KeyChar}" + "\r\n");
    
	//Escape(ESC) 입력시 Text지우기
	if (e.KeyChar == Convert.ToChar(Keys.Escape))
	{
		Control cmp = sender as Control;
		if(cmp != null)
		{
			cmp.Text = null;
			e.Handled = true;
		}
	}
}

//===========================================================

edtSampleControl.KeyPress += OnKeyUp_Sample;
private void OnKeyUp_Sample(object sender, KeyEventArgs e)
{
	Console.WriteLine( $"KeyUp code: {e.KeyCode}, value: {e.KeyValue}, modifiers: {e.Modifiers}" + "\r\n");
}

Visual Studio에서 빌드(Build) 또는 디버그(Debug) 시에 발행하는 경고 오류가 하기와 같은 형식으로 발생할 경우 해결 방법

※ 경고라서 특별히 프로그램 실행하는데 문제는 안되나… 꺼림직해서…

경고    1    포함된 interop 어셈블리 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE.dll'에 대한 참조는 ‘~~~~~~~~~~~~~.dll' 어셈블리가 해당 어셈블리에 대해 만든 간접 참조로 인해 만들어졌습니다. 두 어셈블리 중 하나에서 'Interop 형식 포함' 속성을 변경하십시오.   ~~~~

경고    2    포함된 interop 어셈블리 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE80.dll'에 대한 참조는 ‘~~~~~~~~~~~~~.dll' 어셈블리가 해당 어셈블리에 대해 만든 간접 참조로 인해 만들어졌습니다. 두 어셈블리 중 하나에서 'Interop 형식 포함' 속성을 변경하십시오.    ~~~~~

상기 오류 발생한 경우 프로젝트 참조에서 해당 참조를 선택하여 “Interop 형식 포함(Embed Interop Types)”를 False로 변경한다.

   

출처 : 자작(userpark.net)

무지개참조 : http://documentation.devexpress.com/#WindowsForms/clsDevExpressSkinsSkinManagertopic

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace UserPark
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //DevExpress 보너스 스킨 등록(활성화)
            DevExpress.UserSkins.BonusSkins.Register();
            //DevExpress.XtraEditors.XtraForm(SDI Form) 스킨기능 활성화
            DevExpress.Skins.SkinManager.EnableFormSkins();
            //DevExpress.XtraEditors.XtraForm(MDI Form) 스킨기능 활성화
            DevExpress.Skins.SkinManager.EnableMdiFormSkins();
           
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new TfrmMain());
        }
    }
}

Program.cs 파일에 Line 16~21 코드를 추가한다.

※ 반드시는 아니나…. 추천….

자세한 설명은 생략

  • Main 폼에 아래 소스 추가하면 스킨갤러리를 사용할 수 있다.
    • “DevExpress.XtraBars.RibbonGalleryBarItem” or “DevExpress.XtraBars.Ribbon.GalleryControl” 타입으로 “GalleryItemSkins”명으로 생성(추가) 하였을 경우 아래 코드를 소스에 추가하여 준다.
DevExpress.XtraBars.Helpers.SkinHelper.InitSkinGallery(GalleryItemSkins, true);

  • 스킨 종류 선언
    • Program.cs에 아래 소수 추가(“DevExpress Dark Style” 사용시)    ※반드시는 아니나…. 추천….
DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("DevExpress Dark Style");
// or
DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "DevExpress Dark Style";

DevExpress.LookAndFeel.UserLookAndFeel.Default.UseWindowsXPTheme = false;

참조에 COM객체 “Windows Script Host Object Model”(1.0) 추가

 

   1: string fileName = @"Temp.lnk";
   2: string fileExt = Path.GetExtension(fileName);
   3: if (fileExt.Trim().ToLower() == ".lnk")
   4: {
   5:     IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
   6:     IWshRuntimeLibrary.IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(fileName);
   7:     fileName = link.TargetPath;
   8: }

출처 : 모름

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>


혼합 모드 어셈블리는 런타임의 버전 'v2.0.50727'에 대해 빌드되며 추가 구성 정보 없이 '4.0' 런타임에 로드할 수 없습니다.
라는 오류가 발생할 경우
app.config 파일에 다음 설정을 입력한다.
<startup useLegacyV2RuntimeActivationPolicy="true">

참조 : http://social.technet.microsoft.com/Forums/en-US/ieitprocurrentver/thread/1342e167-8016-4a75-99bf-4d11d01d9bb1


Windows 7 64bit 환경 기준으로 레지스터리 값 수정하면 됨.

(첨부 파일은 또한 Windows 7 64bit 기준으로 작성 됨)

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework]

"EnableIEHosting"=dword:00000001


EnableIEHosting_Windows7_64bit.reg.zip


출처 : http://blog.happydong.kr/189

 

C#으로 개발을 하다보면 다양한 상황에서 디버깅을해서 처리 값을 확인해야 하는 일이 생긴다. Output Value를 확인하다든지... 정확한 값이 세팅되었다 든지..., 더미값(쓰레기값)을 세팅해서 프로그램을 테스트 한다던지... 등등의 여러가지 상황에서 디버깅 값을 확인해야 할 일들이 빈번하게 있기 마련이다. 그리고 이렇게 디버깅한 내용은 릴리즈 할때는 적용이 안되어야 하는 것이 맞다. 그래서 디버깅 코드를 짤때에는 디버그모드일때와 릴리지모드일때 실행되야할 코드를 적절히 나눠서 짜야한다. 이렇게 구분하기 위해서는  #if #endifConditional Attribute를 적절하게 사용하는 것이 좋다. 
처음 개발을 시작했을 때는 생각해보자!! 예전 같았으면 프로그램 소스를 짜면서 디버깅소스를 코드로 같이 짜고 디버그 모드(Debug Mode)에서 확인하고 필요없을때(릴리즈때)는 주석처리하면서 했을 것이다. 물론, 머리가 좋은 사람들은 이런식으로 처리안 했을 것이다. 그러나 프로그램을 처음 접하면서 배우는 것이 디버깅하는 방법보다, 주석을 먼저 배우기때문에 이런 불편한 방법을 사용하는 초보 개발자들이 몇몇 있을 것이다. 물론, 나도 처음 배울때 이랬다.

주석으로 처리하는 잘못된 예

/*  릴리즈때는 사용하지 않음.
            MessageBox.Show("Test");
  */

위 코드의 내용 처럼 릴리즈 모드(Release Mode)일때와 디버그 모드(Debug Mode)일때를 확인해서 처리해야하는 경우가 있다면 바보같이 주석처리해가면서 처리하지말고 #if #end와 Conditional Attribute를 이용하자!!

1. #if #endif 사용

이는 메소드 내부의 간단한 처리를 확인 할때 사용하는 것이 적절한 것 같다. 사용 방법은 아래와 같다.

private void InitializeView()
{

#if DEBUG
	// 처리된 결과의 값을 찍어 봅니다. 
	Debug.WriteLine("Value1");
	Debug.WriteLine("Value2");
#endif
}

#if #endif를 많이 사용하면 코드가 조금 지저분해 질수 있으니 적절하게 적당히 사용하는 것이 좋을 것 같다. 

 

 

2. Conditional Attribute 사용

이는 메소드 형태로 사용할수 있으면 빌드모드에 따라 해당 메소드를 호출할지 않할지를 결정할수 있다. 사용 방법은 아래와 같다.

Conditional Attribute

private void InitializeView()
{
	... 내용 생략...
	DebugMsg();
}
[Conditional("DEBUG")]
public void DebugMsg()
{
	MessageBox.Show("Conditional Test");
}

 

InitializeView메소드안에 보면 DebugMsg라는 메소드를 호출하고 있는데, 빌드모드가 디버그이면 해당 메소드를 호출할 것이고, 릴리즈일 경우 호출되지 않을 것이다. 그리고 Conditional은 다중 지정이 가능하다. 즉, [Conditional("DEBUG"), Conditional("TRACE")] 이런식으로 가능하다.

#if #endif와 Conditional Attribute를 적절히 사용하면 보다 깔끔한 코드, 디버그모드와 릴리지모드를 나눠서 코드를 정리할수 있을 것이다. 물론, #if #endif보다는 Conditional Attribute를 활용하는 쪽이 보다 깔끔한 소스를 짤수 있을 것이다. Conditional를 선언한 메소드들을 한클래스에 모아서 사용하는 것도 적절하다 볼수 있다.

참고 )
프로젝트의 프로퍼티(Properties)메뉴에 들어가, 빌드(Build) 옵션에서 해당 빌드모드의 내용을 체크할수 있다.

 

출처 : http://blog.happydong.kr/189

별출처 : MSDN(http://msdn.microsoft.com/ko-kr/library/bb397679.aspx)

 

 

<?XML:NAMESPACE PREFIX = [default] http://www.w3.org/1999/xhtml NS = "http://www.w3.org/1999/xhtml" />이러한 변환은 예를 들어 명령줄 인수에서 숫자 입력을 가져올 때 유용할 수 있습니다. 비슷한 메서드로 문자열을 float 또는 long 등의 다른 숫자 형식으로 변환할 수도 있습니다. 다음 표에서는 이러한 메서드를 보여 줍니다.

숫자 형식

방법

decimal

ToDecimal(String)

float

ToSingle(String)

double

ToDouble(String)

short

ToInt16(String)

int

ToInt32(String)

long

ToInt64(String)

ushort

ToUInt16(String)

uint

ToUInt32(String)

ulong

ToUInt64(String)

예제

 


이 예제에서는 ToInt32(String) 메서드를 호출하여 입력 stringint로 변환합니다. 프로그램이이 메서드에 의해 throw 될 수 있는 두 개의 가장 일반적인 예외를 catch 합니다. FormatExceptionOverflowException. 정수 저장소 위치를 오버플로하지 않고 숫자를 증가시킬 수 있는 경우 프로그램에서 결과에 1을 더하여 출력을 인쇄합니다.

C#

static void Main(string[] args)
{
    int numVal = -1;
    bool repeat = true;

    while (repeat == true)
    {
        Console.WriteLine("Enter a number between −2,147,483,648 and +2,147,483,647 (inclusive).");

        string input = Console.ReadLine();

        // ToInt32 can throw FormatException or OverflowException.
        try
        {
            numVal = Convert.ToInt32(input);
        }
        catch (FormatException e)
        {
            Console.WriteLine("Input string is not a sequence of digits.");
        }
        catch (OverflowException e)
        {
            Console.WriteLine("The number cannot fit in an Int32.");
        }
        finally
        {
            if (numVal < Int32.MaxValue)
            {
                Console.WriteLine("The new value is {0}", numVal + 1);
            }
            else
            {
                Console.WriteLine("numVal cannot be incremented beyond its current value");
            }
        }
        Console.WriteLine("Go again? Y/N");
        string go = Console.ReadLine();
        if (go == "Y" || go == "y")
        {
            repeat = true;
        }
        else
        {
            repeat = false;
        }
    }
    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();    
}
// Sample Output:
// Enter a number between -2,147,483,648 and +2,147,483,647 (inclusive).
// 473
// The new value is 474
// Go again? Y/N
// y
// Enter a number between -2,147,483,648 and +2,147,483,647 (inclusive).
// 2147483647
// numVal cannot be incremented beyond its current value
// Go again? Y/N
// Y
// Enter a number between -2,147,483,648 and +2,147,483,647 (inclusive).
// -1000
// The new value is -999
// Go again? Y/N
// n
// Press any key to exit.

string 을 int로 변환하는 또 다른 방법은 System.Int32 구조체의 Parse 또는 TryParse 메서드를 사용하는 것입니다. ToUInt32 메서드는 Parse를 내부적으로 사용합니다. 문자열이 올바른 형식이 아닌 경우 Parse는 예외를 throw하는 반면, TryParse는 예외를 throw하지 않고false를 반환합니다. 다음 예제에서는 ParseTryParse에 대한 호출이 성공하는 경우와 실패하는 경우를 모두 보여 줍니다.

C#

int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
// Output: -105

C#

// TryParse returns true if the conversion succeeded
// and stores the result in the specified variable.
int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");
// Output: -105

C#

try
{
    int m = Int32.Parse("abc");
}
catch (FormatException e)
{
    Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format.

C#

string inputString = "abc";
int numValue;
bool parsed = Int32.TryParse(inputString, out numValue);

if (!parsed)
    Console.WriteLine("Int32.TryParse could not parse '{0}' to an int.\n", inputString);

// Output: Int32.TryParse could not parse 'abc' to an int.

별Link : How to Integrate Excel in a Windows Form Application using the WebBrowser

 

 

Sample Image - Embedding_Excel.jpg

Introduction

With Automation, you are able to drive any Office application from your .NET application. This is really powerful. It may happen that one time, you would like to integrate such an application (Excel, for example) in your own application, and handle it like a control. A first approach has already been published on The Code Project (see the Background section in this article). The other method I will describe here uses the Microsoft WebBrowser control as a host for the document.

Background

You can study Anup Shinde's article. His method works fine. Instead of the WebBrowser control, it is based on Windows Win32 API.

If you are interested in the original publication of the WebBrowser method, you can see the Microsoft KB.

Starting from scratch

Create a new form MyForm, and add a new WebBrowser control, named webBrowser1. Add the m_ExcelFileNamefield :

Collapse | Copy Code

// Contains the path to the workbook file
private string m_ExcelFileName="test.xls"; // Replace here with an existing file

Then create a function OpenFile like this :

Collapse | Copy Code

public void OpenFile(string filename) 
{
    // Check the file exists
    if(!System.IO.File.Exists(filename)) throw new Exception();
        m_ExcelFileName=filename;
    // Load the workbook in the WebBrowser control
    this.webBrowser1.Navigate(filename,false);
}

You can try and run your application, giving the filename an existing excel file path. You'll see that it works perfectly. Really easy, don't you think?

In fact, you will quickly get into trouble if you try to run the application a second time with the same Excel file. An error message tells you that your file is already in use. This may be strange because you think that you closed your application, and you did. So where is the problem?

Let's see what happened in the background. While the WebBrowser was navigating, it opened an invisible Excel application, and loaded the workbook inside it. And when you closed your application, the WebBrowser didn't close either its Excel application or the workbook. So we must do it, and this is the most difficult part of our job.

Solving the problem step by step

Before further reading, you have to load the following Office COM library references for Office Automation :

  • Microsoft Excel 11.0 Object Library
  • Microsoft Office 11.0 Object Library

and use them in your file :

Collapse | Copy Code

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

You'll need these assemblies too :

Collapse | Copy Code

using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;

Declare these two Excel fields :

Collapse | Copy Code

// Contains a reference to the hosting application
private Microsoft.Office.Interop.Excel.Application m_XlApplication=null;
// Contains a reference to the active workbook
private Workbook m_Workbook=null;

Before trying to close the workbook, we need a handle on it. For convenience, the best moment to do this is just after the document has been loaded in the WebBrowser. So we have to generate a webBrowser1_Navigated event handler and its matching function, like this :

Collapse | Copy Code

private void webBrowser1_Navigated(object sender,WebBrowserNavigatedEventArgs e) 
{
    // Creation of the workbook object
    if((m_Workbook=RetrieveWorkbook(m_ExcelFileName))==null)return;
    // Create the Excel.Application
    m_XlApplication=(Microsoft.Office.Interop.Excel.Application)m_Workbook.Application;
}

Then we define the RetrieveWorkbook function. It is based on two imported Win32 API functions, that retrieve all the programs that are running on our computer. Our job is to search among them the one that is working with the workbook that names xlfile. The code is like this :

Collapse | Copy Code

[DllImport("ole32.dll")] static extern int GetRunningObjectTable
                (uint reserved,out IRunningObjectTable pprot);
[DllImport("ole32.dll")] static extern int CreateBindCtx(uint reserved,out IBindCtx pctx);

public Workbook RetrieveWorkbook(string xlfile) 
{
        IRunningObjectTable prot=null;
        IEnumMoniker pmonkenum=null;
        try 
        {
            IntPtr pfetched=IntPtr.Zero;
            // Query the running object table (ROT)
            if(GetRunningObjectTable(0,out prot)!=0||prot==null) return null;
            prot.EnumRunning(out pmonkenum); pmonkenum.Reset();
            IMoniker[] monikers=new IMoniker[1];
            while(pmonkenum.Next(1,monikers,pfetched)==0) 
            {
                IBindCtx pctx; string filepathname;
                CreateBindCtx(0,out pctx);
                 // Get the name of the file
                 monikers[0].GetDisplayName(pctx,null,out filepathname);
                 // Clean up
                 Marshal.ReleaseComObject(pctx);
                 // Search for the workbook
                 if(filepathname.IndexOf(xlfile)!=-1) 
                 {
                     object roval;
                     // Get a handle on the workbook
                     prot.GetObject(monikers[0],out roval);
                     return roval as Workbook;
                 }
              }
         } 
         catch 
         {
             return null;
         } 
         finally 
         {
             // Clean up
             if(prot!=null) Marshal.ReleaseComObject(prot);
             if(pmonkenum!=null) Marshal.ReleaseComObject(pmonkenum);
         }
         return null;
}

Now we can write the code involved to close the background Excel application, while overriding the OnClose() event :

Collapse | Copy Code

protected override void OnClosed(object sender, EventArgs e) 
{
    try 
    {
        // Quit Excel and clean up.
        if(m_Workbook!=null) 
        {
            m_Workbook.Close(true,Missing.Value,Missing.Value);
            System.Runtime.InteropServices.Marshal.ReleaseComObject
                                    (m_Workbook);
            m_Workbook=null;
        }
        if(m_XlApplication!=null) 
        {
            m_XlApplication.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject
                                (m_XlApplication);
            m_XlApplication=null;
            System.GC.Collect();
        }
    } 
    catch 
    {
        MessageBox.Show("Failed to close the application");
    }
}

Using the code

You can use the code as written upper. Otherwise, it may be interesting to embed all the stuff in a .NET control. You'll be able to manage CommandBars, Menus, etc. inside the control. You will find some code in the downloadable package section.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Temporary Directory

string strTempPath = System.IO.Path.GetTempPath();

 

Temporary FileName

string strTempFileName = System.IO.Path.GetTempFileName();

오류가 발생된 환경은 아래와 같으며, 아래 환경기준으로 설명하겠습니다.

  • OS : Microsoft Windows 7 Pro. 64bit
  • VisualStudio 2012
  • Team Foundation Server 2012

PC명을 변경하였을시 Team Foundation Server와 VisualStudio 팀프로젝트 이용시 오류가 발생 된다.

 

해당 솔루션을 오픈 시 PC명이 변경되었으니 “tf workspaces /updateComputerName:oldComputerName” 명령을 이용하여 변경하라고 한다.

(이 부분은 벌써 처리한 후라서 기억이 가물가물합니다.)

 

실행창을 열어 “cmd.exe”를 실행합니다.

저 명령대로 “tf workspaces”라고 실행하여 보면 경로를 찾을 수 없다고 나옵니다.

사용자별로 틀리겠지만 “Windows 7 64bit”환경에서는 “C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\” 경로에 tf.exe가 있습니다.

 

그래서 전체 경로를 포함한 명령을 실행하여 현재 Workspace들을 확인합니다.

C:\>"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\tf" workspaces

변경하기 위해서는

C:\>"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\tf" workspaces /updateComputerName:{이전 PC명}

이라고 하면 여기서도 오류를 발생합니다.

/updateComputerName 또는 /updateUserName을 지정할 때에는 /collection 옵션을 지정해야 합니다.

그럼 최종 명령은

"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\tf" workspaces /updateComputerName:JHPARK-NOTE /collection:http://tfs2012.hi-techeng.co.kr:8080/tfs/HITECHENG

C:\>"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\tf" workspaces /updateComputerName:{이전 PC명} /collection:http://{Team Foondation Server URL}:{Port(기본:8080)}/tfs/{팀 프로젝트 컬렉션}

그 이후 이전 PC명으로 된 Workspace를 절대 삭제하지 않는것을 권장합니다.

혹여 삭제를 하였을 경우 새로 솔루션을 소수제어에 추가하고 충돌해결을 해야 하는 번거러운 작업을 경험하게 되실 겁니다.(제가 그랬습니다.)

대략 적으로 설명을 하였는데 이해가 되었는지 모르겠습니다. 저만의 방식과 해결후 정리 개념으로 작성하다보니…

 

기타 참고 문헌 :

(한글) http://msdn.microsoft.com/ko-kr/library/54dkh0y3%28v=vs.100%29   

(영문) http://msdn.microsoft.com/en-us/library/54dkh0y3%28v=vs.100%29.aspx

사전에 Exchange Web Services Managed API(SDK)가 설치 되어져 있어야 하며

프로젝트 참조에 Microsoft.Exchange.WebServices (C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll)를 추가하시기 바랍니다.

using Microsoft.Exchange.WebServices.Data;

본 샘플은 제목만 읽어온것이며, 경우에 따라서는 메소드를 잘 활용하시면 여러가지 정보를 읽어 올 수 있습니다.

소스만 보셔도 쉽게 이해가 가능 할 실 겁니다.

[????] 된 부분은 사용자에 맞게 변경하시기 바랍니다.

Exchange 서버 버전은 2010 SP1입니다.

8라인에서 서버에 맞는 버전을 선택하여 주세요. (2010 SP1은 ExchangeVersion.Exchange2010_SP1)

16라인에서 1개만 가져올경우 샘플이며, 크기는 알아서 조절 하세요(new ItemView(1))

string strExchangeDomain = @"[Domain]"; //ex) userpark.net
string strExchangeServerName = string.Format("exchange.{0}", strExchangeDomain); // exchange.userpark.net
string strExchangeUserID = @"[UserID]"; //사용자 ID
string strExchangeUserPs = @"[Password]"; //사용자 Password
string strExchangeMail = string.Format("{0}@{1}", strExchangeUserID, strExchangeDomain);  //ex)userpark@userpark.net
string strExchangeEwsUrl = string.Format("{0}://{1}/EWS/Exchange.asmx", "https", strExchangeServerName);

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

//두개중 하나 NetworkCredential 또는 WebCredentials
//service.Credentials = new System.Net.NetworkCredential(strExchangeUserID, strExchangeUserPs, strExchangeDomain);
service.Credentials = new WebCredentials(strExchangeUserID, strExchangeUserPs, strExchangeDomain);

service.Url = new Uri(strExchangeEwsUrl);

Microsoft.Exchange.WebServices.Data.FindItemsResults<Microsoft.Exchange.WebServices.Data.Item> findResults = service.FindItems(Microsoft.Exchange.WebServices.Data.WellKnownFolderName.Inbox, new ItemView(1));
foreach (Microsoft.Exchange.WebServices.Data.Item item in findResults.Items)
{
	//if (item.IsNew == true)  //읽지 않은 경우만 가져올 경우
	Console.WriteLine(item.Subject);
}

 

2012/06/25 - [자료실] - Microsoft Exchange Web Service Managed API SDK 1.2.1

출처 : 자작(userpark)

+ Recent posts