반응형

* C# 프로그램 재시작 예제...

 

메인 화면

 

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace CSharp_ProgramReStart
{
    public partial class Form1 : Form
    {

        #region INI 파일 사용을 위해 API 선언...

        [DllImport("KERNEL32.DLL")]
        private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

        [DllImport("KERNEL32.DLL")]
        private static extern uint GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);

        [DllImport("kernel32.dll")]
        static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

        #endregion

        string strINIPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\')) + "\\INI";
        int iCount = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //INI File 만들기...
            CreateIni("ReStart");

            //INI File Load...
            label2.Text = getIni("Restart_Info", "restart", "0", strINIPath + "\\Restart.ini");
            iCount = Convert.ToInt32(label2.Text);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            iCount += 1;
            setIni("Restart_Info", "restart", iCount.ToString(), strINIPath + "\\Restart.ini");

            Application.Exit();
            //프로그램 다시 시작 하기... 1초 후
            System.Threading.Thread.Sleep(1000);
            //1번째 방법...
            //Application.Restart();
            //2번째 방법...
            System.Diagnostics.Process.Start(Application.ExecutablePath);
        }


        #region 사용자 정의 함수...

        //INIFile 읽어오기...
        private string getIni(string IpAppName, string IpKeyName, string lpDefalut, string filePath)
        {
            string inifile = filePath;    //Path + File

            try
            {
                StringBuilder result = new StringBuilder(255);
                GetPrivateProfileString(IpAppName, IpKeyName, lpDefalut, result, result.Capacity, inifile);

                return result.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return "실패";
            }
        }

        //INIFile 쓰기...
        private Boolean setIni(string IpAppName, string IpKeyName, string IpValue, string filePath)
        {
            try
            {
                string inifile = filePath;  //Path + File
                WritePrivateProfileString(IpAppName, IpKeyName, IpValue, inifile);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        //INIFile 만들기...
        private Boolean CreateIni(string strFileName)
        {
            try
            {
                string strCheckFolder = "";

                strCheckFolder = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\'));
                strCheckFolder += "\\INI";
                if (!System.IO.Directory.Exists(strCheckFolder))
                {
                    System.IO.Directory.CreateDirectory(strCheckFolder);

                }

                strCheckFolder += "\\" + strFileName + ".ini";
                if (!System.IO.File.Exists(strCheckFolder))
                {
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(strCheckFolder, true, Encoding.GetEncoding(949)))
                    {
                        sw.Write("\r\n");
                        sw.Flush();
                        sw.Close();
                    }

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return false;
            }
            return true;
        }


        #endregion

    }
}

 

* 예제 결과 

 

 

 

 

https://kdsoft-zeros.tistory.com/31

 

[C#] INI File Create & Read & Write

* INI 파일 예제... Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System...

kdsoft-zeros.tistory.com

 

반응형
반응형

위의 그림과 같이 왼쪽 res -> menu 폴더에서 오른쪽 마우스 버튼을 클릭 하여 xml 파일을 생성 한다.

xml 이름은 각자 알아서 정의 하면 됩니다. 단! menu 폴더가 없을 시 안드로이드 프로젝트 생성된 폴더

로 가셔서 아래는 제 프로젝트 만든 곳 임.

C:\Users\Administrator\AndroidStudioProjects\VisualProgramming\app\src\main\res

탐색기를 열어 직접 위의 경로로 이동 하여 res 폴더에 menu 폴더를 생성 해 줍니다.

 

아래의 그림과 같이 xml 생성 후 클릭 하여 탭에 내용에 들어 가보면

 

<item android:id="@+id/menu_log"

       android:title="로그 정보"

       app:showAsAction="never">             

 

메뉴 아이템을 하나 추가 하여 저장.

참고로 item 부분에 showAsAction 부분은 

app:showAsAction="always" : 항상 보이게 표시

app:showAsAction="never" : 항상 overflow 에 표시

app:showAsAction="ifRoom" : 액션바에 공간이 있을경우 표시

app:showAsAction="withText" : 액션바에 아이콘과 텍스트 함께 표시

이렇게 구성 됩니다.

 

이제 xml 내용은 다 끝났고 메뉴를 추가할 소스 부분에 아래와 같이 두 오버로드 된 함수를 포함 시켜 줍니다.

 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);       <-R.menu.menu - xml 파일 이름을 적으면 됩니다.

    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
    int id = menuItem.getItemId();
    if (id == R.id.menu_log)               <-R.id.menu_log - xml에 정의된 item Id 를 적으면 됩니다.
    {
        Toast.makeText(getApplicationContext(),"로그 정보 클릭...",Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(menuItem);
}

반응형
반응형

* VBNET 다른 응용 프로그램 실행 및 종료 예제...

 

메인 화면

전체 소스 코드

Form1.vb

 

Public Class Form1

    Dim pcEXE As System.Diagnostics.Process


    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '파일 열기...
        Dim ofd As OpenFileDialog = New OpenFileDialog()

        'EXE 파일만 열기...
        ofd.Filter = "EXE File (*.exe) | *.exe"

        If (ofd.ShowDialog() = Windows.Forms.DialogResult.OK) Then

            label1.Text = ofd.FileName

        End If

    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        '응용 프로그램 실행 시키기...
        '해당 파일이 존재 하지 않으면...
        If Not System.IO.File.Exists(label1.Text) Then
            Return
        End If

        '다른 응용 프로그램 실행 시키기...
        pcEXE = System.Diagnostics.Process.Start(label1.Text)

    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        '응용 프로그램 죽이기...
        If pcEXE Is Nothing Then Return
        '죽이기...
        pcEXE.Kill()
    End Sub

End Class

 

* 예제 결과

 

다른 응용 프로그램 실행 결과 화면

 

 

https://kdsoft-zeros.tistory.com/69

 

[C#] 다른 응용 프로그램 실행 및 종료

* C# 다른 응용 프로그램 실행 및 종료 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# 다른 응용 프로그램 실행 및 종료 예제...

 

메인화면

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_다른응용프로그램실행시키기
{
    public partial class Form1 : Form
    {
        System.Diagnostics.Process pc;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            
            //exe 파일 만 열기...
            ofd.Filter = "EXE File (*.exe) | *.exe";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                label1.Text = ofd.FileName;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //파일이 없다면...
            if (!System.IO.File.Exists(label1.Text)) return;

            //다른 응용 프로그램 실행 시키기...
            pc = System.Diagnostics.Process.Start(label1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pc == null) return;

            //실행 시킨 프로그램 죽이기...
            pc.Kill();
        }
    }
}

* 예제 결과

 

다른 응용 프로그램 실행 결과 화면

 

반응형
반응형

*  Activity 화면 고정 

- Activity 에서 onCreate 함수 안에

 아래의 그림과 같이 세로 모드 또는 가로 모드 를 써 줍니다.

 

 

반응형
반응형

* VBNET 파일 사용 가능 여부 체크 예제...

 

메인 화면

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '파일 열기...
        Dim ofd As OpenFileDialog = New OpenFileDialog()

        If (ofd.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            '선택된 파일 표시...
            label1.Text = ofd.FileName
        End If

    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        '파일 사용 유무 체크...
        Dim strErr As String = ""

        If (FileIsUse(label1.Text, strErr)) Then

            label2.Text = "사용 가능한 파일 입니다."

        Else

            label2.Text = "파일 사용중.., " + strErr

        End If


    End Sub


    Private Function FileIsUse(ByVal strFilePath As String, ByRef strErr As String) As Boolean

        Try
            Using fs As System.IO.FileStream = New System.IO.FileStream(strFilePath, _
                                                            System.IO.FileMode.Open, _
                                                            System.IO.FileAccess.Read, _
                                                            System.IO.FileShare.Read)
                '정상적으로 오픈된 파일 다시 닫기...
                fs.Close()

            End Using


        Catch ex As Exception
            strErr = ex.Message.ToString()
            Return False
        End Try

        Return True
    End Function

End Class

 

* 예제 결과

 

파일이 정상적으로 사용 가능 할 때

 

해당 파일을 다른 프로그램에서 사용 중일때

 

 

 

https://kdsoft-zeros.tistory.com/66

 

[C#] File 사용 가능 여부 체크

* C# 파일 사용 가능 여부 체크 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# 파일 사용 가능 여부 체크 예제...

 

메인 화면

 

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_FileIsUse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //파일 선택...
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //선택된 파일 표시...
                label1.Text = ofd.FileName;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //파일 사용 유무...
            string strErr = "";


            if (FileIsUse(label1.Text, ref strErr))
            {
                label2.Text = "사용 가능한 파일 입니다.";
            }
            else
            {
                label2.Text = "파일 사용중.., " + strErr ;
            }


        }

        private bool FileIsUse(string strFilePath, ref string strErr)
        {

            try
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(strFilePath, 
                                                                          System.IO.FileMode.Open, 
                                                                          System.IO.FileAccess.Read, 
                                                                          System.IO.FileShare.Read))
                {
                    //파일 닫기...
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                strErr = ex.Message.ToString();
                return false;
            }


            return true;
        }

    }
}

 

* 예제 결과

 

 

반응형
반응형

* VBNET XML File Write & Read 예제...

 

메인화면

전체 소스 코드

Form1.vb

 

Public Class Form1

    Dim strLocal As String = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\"))
    Dim strXMLFile As String = "\XMLText.xml"

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        'XMLFile Write

        '파일이 존재 하면은... 삭제하고 다시...
        If System.IO.File.Exists(strLocal + strXMLFile) Then
            System.IO.File.Delete(strLocal + strXMLFile)
        End If

        'XML Create
        Dim xdDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        '최상위 노드 생성...
        Dim xnRoot As System.Xml.XmlNode = xdDoc.CreateElement("테스트")

        'xmRoot 에 속한 하위 노드...
        Dim xnTmp As System.Xml.XmlNode = xdDoc.CreateElement("XMLFileEx")
        xnTmp.InnerText = "XML Test"

        '최상위 노드에 하위노드 xmTmp 추가... 
        xnRoot.AppendChild(xnTmp)

        'CheckBox 저장부...
        Dim xnCheckBox As System.Xml.XmlNode = xdDoc.CreateElement("ProgramCheckBox")
        Dim xaCheckBox1 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("CheckBox1")
        Dim xaCheckBox2 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("CheckBox2")
        Dim xaCheckBox3 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("CheckBox3")

        '값 대입...
        xaCheckBox1.Value = checkBox1.Checked.ToString()
        xaCheckBox2.Value = checkBox2.Checked.ToString()
        xaCheckBox3.Value = checkBox3.Checked.ToString()

        'xnCheckBox 노드의 속성으로 추가...
        xnCheckBox.Attributes.Append(xaCheckBox1)
        xnCheckBox.Attributes.Append(xaCheckBox2)
        xnCheckBox.Attributes.Append(xaCheckBox3)

        '최상위 노드의 하위 노드로 추가 
        xnRoot.AppendChild(xnCheckBox)

        'Radio 저장부...
        Dim xnRadio As System.Xml.XmlNode = xdDoc.CreateElement("ProgramRadio")
        Dim xaRadio1 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("Radio1")
        Dim xaRadio2 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("Radio2")
        Dim xaRadio3 As System.Xml.XmlAttribute = xdDoc.CreateAttribute("Radio3")

        '값 대입...
        xaRadio1.Value = radioButton1.Checked.ToString()
        xaRadio2.Value = radioButton2.Checked.ToString()
        xaRadio3.Value = radioButton3.Checked.ToString()

        'xnRadio 노드의 속성으로 추가...
        xnRadio.Attributes.Append(xaRadio1)
        xnRadio.Attributes.Append(xaRadio2)
        xnRadio.Attributes.Append(xaRadio3)

        '최상위 노드의 하위 노드로 추가
        xnRoot.AppendChild(xnRadio)

        'XML File 로 저장
        xdDoc.AppendChild(xnRoot)
        xdDoc.Save(strLocal + strXMLFile)

    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        'XMLFile Read

        '파일이 존재 하지 않으면...
        If Not System.IO.File.Exists(strLocal + strXMLFile) Then
            Return
        End If

        textBox1.Text = ""
        Dim xdDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        'XML File Load
        xdDoc.Load(strLocal + strXMLFile)

        '최상위 노드 1개에 하위 노드를 추가한 부분 읽기...
        For Each xn As System.Xml.XmlNode In xdDoc.ChildNodes

            textBox1.Text += "RootNode = " + xn.Name + System.Environment.NewLine
            textBox1.Text += "하위 노드" + System.Environment.NewLine

            For Each xx As System.Xml.XmlNode In xn

                If xx.Name = "XMLFileEx" Then
                    textBox1.Text += "RootNode ->" + xx.Name + System.Environment.NewLine
                    textBox1.Text += "           " + xx.InnerText + System.Environment.NewLine
                ElseIf xx.Name = "ProgramCheckBox" Then
                    textBox1.Text += "RootNode ->" + xx.Name + System.Environment.NewLine
                    textBox1.Text += "           CheckBox1 = " + xx.Attributes(0).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           CheckBox2 = " + xx.Attributes(1).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           CheckBox3 = " + xx.Attributes(2).Value.ToString() + System.Environment.NewLine
                Else
                    textBox1.Text += "RootNode ->" + xx.Name + System.Environment.NewLine
                    textBox1.Text += "           Radio1 = " + xx.Attributes(0).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           Radio2 = " + xx.Attributes(1).Value.ToString() + System.Environment.NewLine
                    textBox1.Text += "           Radio3 = " + xx.Attributes(2).Value.ToString() + System.Environment.NewLine
                End If

            Next

        Next

    End Sub

End Class

* 예제 결과

 

- 파일 저장 위치 및 파일 내용

 

아래의 그림은 XML 파일 만들기 버튼 클릭 이후 메모장으로 XML 파일 열어본 결과

파일 내용

 

결과 화면

 

https://kdsoft-zeros.tistory.com/64

 

[C#] XML File Write & Read 예제

* C# XML File Write & Read 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; u..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts