但是如果使用者沒有這個資料夾該怎麼辦呢?
方法如下:
using System.IO;
if (Directory.Exists(@"C:\test"))
{
//資料夾存在
}
else
{
//資料夾不存在,建立資料夾
Directory.CreateDirectory(@"C:\test");
}
using System.IO;
if (Directory.Exists(@"C:\test"))
{
//資料夾存在
}
else
{
//資料夾不存在,建立資料夾
Directory.CreateDirectory(@"C:\test");
}
private void UseHostNameToFindIP(string hostName)
{
IPAddress[] getIP;
try
{
//利用電腦名稱取得IP
getIP = Dns.GetHostAddresses(str_ClientHostName[i]);
//搜尋清單內的所有IP,因為會包含IPV6跟IPV4
for (int j = 0; j < getIP.Length; j++)
{
if (IPAddress.Parse(getIP[j].ToString()).AddressFamily
== AddressFamily.InterNetwork) //只找出IPV4的IP
{
Console.WriteLine(getIP[j].ToString());
}
}
}
catch (SocketException)
{
//找不到此電腦名稱就將IP設定成127.0.0.1
str_ContentClientIP[i] = "127.0.0.1";
}
}
private void GetIP()
{
string hostName = Dns.GetHostName(); //取得本機名稱
//取得所有IP,包含IPV4和IPV6
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);
foreach (IPAddress ip in addressList)
{
Console.WriteLine(ip.ToString());
}
Console.ReadLine();
}
以上的IP包含所有網卡(虛擬網卡)的IPV4和IPV6的IPprivate void GetIP()
{
string hostName = Dns.GetHostName(); //取得本機名稱
//取得所有IP,包含IPV4和IPV6
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);
foreach (IPAddress ip in addressList)
{
//過濾IPV4的位址
if (ip.AddressFamily == AddressFamily.InterNetwork)
Console.Writeline(ip.ToString());
}
Console.ReadLine();
}
以上包含所有網卡(虛擬網卡)的IPV4class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetLocalIP());
Console.WriteLine("-- -- The End -- --");
Console.ReadKey();
}
public static string GetLocalIP()
{
string result = RunApp("route", "print", true);
Match m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
if (m.Success)
{
return m.Groups[2].Value;
}
else
{
return "找不到本機IP";
}
}
public static string RunApp(string filename, string arguments, bool recordLog)
{
try
{
if (recordLog)
{
Trace.WriteLine(filename + " " + arguments);
}
Process proc = new Process();
proc.StartInfo.FileName = filename;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
using (System.IO.StreamReader sr =
new System.IO.StreamReader(proc.StandardOutput.BaseStream,
Encoding.Default))
{
string txt = sr.ReadToEnd();
sr.Close();
if (recordLog)
{
Trace.WriteLine(txt);
}
if (!proc.HasExited)
{
proc.Kill();
}
return txt;
}
}
catch (Exception ex)
{
Trace.WriteLine(ex);
return ex.Message;
}
}
}
以上就能過濾掉其他討厭的IP位址啦!
using System.Diagnostics;
namespace OpenFile
{
class Program
{
static void Main(string[] args)
{
//欲開啟的檔案路徑
Process.Start(@"D:\test.exe");
}
}
}