1 申请开发者帐号
2 申请APP ID
https://developer.apple.com/ios/manage/bundles/index.action
Description:San Zhang
Bundle Identifier (App ID Suffix): cn.a.appname
3 申请CSR
钥匙串访问-证书助理-从证书颁发机构求证书
填写注册开发账户时的邮箱和用户名,将CSR文件存储到磁盘。
4 申请发布证书
iOS provisioning portal-certificates-distribution-导入CSR文件-download-打开-在钥匙串访问中显示
5 申请distribution provisioning
iOS provisioning portal-provisioning-distribution-add new-submit-download-打开-自动关联
6 itunes connect中新增app,填写相关信息。
7 代码中新增Entitlements.plist文件
内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>application-identifier</key>
<string>Z3VJF32UKJ.cn.a.appname</string>
<key>get-task-allow</key>
<false/>
</dict>
</plist>
8 修改resource下的plist文件
Bundle identifier:cn.a.appname
9 配置xcode
项目-get info-configurations中新增distribution配置项。
Build选项-configuration选择为distribution-修改codesigning identify和any iphone os device为distribution证书。Targeted device family为iphone。
Target中-get info-做同上设置
10 clean,build and archive。
注意:文件目录层次不要太深。文件名长度进行控制。
11 打开xcode-organizer-archived applications-选择编译程序,validate-submit
1 IIS中网站属性--HTTP头--启用内容过期
2 swf所在的html head中添加
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="0">
参考:
swf 更新后,客户端不需要清空缓存即可查看最新版本
数据库读取工具类
1 package com.db;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import oracle.jdbc.driver.OracleDriver;
7
8 import javax.naming.Context;
9 import javax.naming.InitialContext;
10 import javax.naming.NamingException;
11 import javax.sql.DataSource;
12
13 /**
14 * 数据库连接工具类
15 * @version 1.0
16 */
17 public class JdbcConnection {
18
19 // 定义线程本地变量,每个线程访问它都会获得不同的对象
20 // 使用ThreadLocal使一个连接绑定到一个线程上
21 private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
22 private static String username=null; //用户名
23 private static String password=null; //密码
24 private static String dbName=null; //数据库名称
25 private static String ip=null; //数据库服务器IP地址
26 private static String resourceName=null; //为null时不使用连接池, jdbc/mysql或jdbc/oracle或jdbc/derby
27 private static String databaseType = "oracle";
28
29 private static void initParams(){
30 username=DbConfig.getInstance().getDb_username();
31 password=DbConfig.getInstance().getDb_password();
32 dbName=DbConfig.getInstance().getDb_name();
33 ip=DbConfig.getInstance().getIp();
34 }
35
36 /**
37 *
38 * @return 得到一个数据库连接
39 * @throws SQLException
40 */
41 public static Connection getConnection() throws SQLException {
42 Connection conn = currentConnection.get();
43 if (conn == null) {
44 if(null==resourceName){
45 if("mysql".equals(databaseType.toLowerCase())){
46 conn = getMySqlConnection();
47 }else if("oracle".equals(databaseType.toLowerCase())){
48 conn = getOracleConnection();
49 }else if("derby".equals(databaseType.toLowerCase())){
50 conn = getDerbyConnection();
51 }else{
52 System.out.println("在 JdbcConnection.java 中数据库类型没有设置");
53 throw new SQLException("数据库类型未设置");
54 }
55 }else{
56 conn = getConnectionByPool();
57 }
58 currentConnection.set(conn);
59 }
60 return conn;
61 }
62 /**
63 * 关闭Oracle数据库连接
64 * @throws SQLException
65 */
66 public static void closeConnection() throws SQLException{
67 Connection conn = currentConnection.get();
68 conn.close();
69 currentConnection.set(null);
70 }
71 //获得Oracle数据库连接
72 private static Connection getOracleConnection(){
73 initParams();
74 Connection conn = null;
75 try {
76 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); //加载驱动
77 conn= DriverManager.getConnection("jdbc:oracle:thin:@"+ip+":1521:"+dbName,username,password);
78 } catch (ClassNotFoundException e) {
79 e.printStackTrace();
80 System.out.println("Oracle驱动没找到");
81 } catch (InstantiationException e) {
82 e.printStackTrace();
83 } catch (IllegalAccessException e) {
84 e.printStackTrace();
85 } catch (SQLException e) {
86 e.printStackTrace();
87 }
88 return conn;
89 }
90 //获得MySql数据库连接
91 private static Connection getMySqlConnection(){
92 initParams();
93 Connection conn = null;
94 try {
95 Class.forName("com.mysql.jdbc.Driver").newInstance(); //加载驱动
96 String url = "jdbc:mysql://"+ip+":3306/"+dbName+"?useUnicode=true&characterEncoding=utf8";
97 conn = DriverManager.getConnection(url, username, password);
98 } catch (ClassNotFoundException e) {
99 e.printStackTrace();
100 System.out.println("MySql驱动没找到");
101 } catch (SQLException e) {
102 e.printStackTrace();
103 } catch (InstantiationException e) {
104 e.printStackTrace();
105 } catch (IllegalAccessException e) {
106 e.printStackTrace();
107 }
108 return conn;
109 }
110 //获取Derby数据库连接
111 private static Connection getDerbyConnection(){
112 initParams();
113 Connection conn = null;
114 try {
115 Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); //加载驱动
116 String url = "jdbc:derby://"+ip+":1527/"+dbName+";create=true";
117 conn = DriverManager.getConnection(url, username, password);
118 } catch (ClassNotFoundException e) {
119 e.printStackTrace();
120 System.out.println("Derby驱动没找到");
121 } catch (SQLException e) {
122 e.printStackTrace();
123 } catch (InstantiationException e) {
124 e.printStackTrace();
125 } catch (IllegalAccessException e) {
126 e.printStackTrace();
127 }
128 return conn;
129 }
130 //获取连接池连接
131 private static Connection getConnectionByPool(){
132 try {
133 Context ctx = new InitialContext();
134 Context subContext = (Context)ctx.lookup("java:comp/env");
135 String dsName="";
136 dsName = resourceName;
137
138 DataSource dataSource = (DataSource)subContext.lookup(dsName);
139 //上面两句可以合写成下边这句
140 //ctx.lookup("java:comp/env/jdbc/oracle");// java:comp/env/ 规定:加前缀指定资源
141 return dataSource.getConnection();
142 }
143 catch (NamingException e) {e.printStackTrace();}
144 catch (SQLException e) {e.printStackTrace();}
145 return null;
146 }
147 }
读取.properties文件
1 package com.db;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.net.URISyntaxException;
8 import java.util.Properties;
9
10 /**
11 * 数据库访问配置文件各参数的获取
12 * @author lzx
13 *
14 */
15 public class DbConfig {
16 //数据库及server配置文件路径
17 private static final String ACTIONPATH = "config.properties";
18 private static DbConfig instance=null;
19
20 private String db_username=null;
21 private String db_password=null;
22 private String db_name=null;
23 private String ip=null;
24 private String ags_user=null;
25 private String ags_password=null;
26 private String ags_domain=null;
27 private String ags_host=null;
28 private String ags_servicename=null;
29
30 private DbConfig(){}
31
32 public String getDb_username() {
33 return db_username;
34 }
35 public String getDb_password() {
36 return db_password;
37 }
38 public String getDb_name() {
39 return db_name;
40 }
41 public String getIp() {
42 return ip;
43 }
44 public String getAgs_user() {
45 return ags_user;
46 }
47 public String getAgs_password() {
48 return ags_password;
49 }
50 public String getAgs_domain() {
51 return ags_domain;
52 }
53 public String getAgs_host() {
54 return ags_host;
55 }
56 public String getAgs_servicename() {
57 return ags_servicename;
58 }
59
60 public static DbConfig getInstance(){
61 if(instance==null){
62 instance= new DbConfig().getNewDbConfig();
63 }
64 return instance;
65 }
66
67 private DbConfig getNewDbConfig(){
68
69 DbConfig dc=new DbConfig();
70 Properties prop = new Properties();
71 String path=null;
72 FileInputStream fis=null;
73
74 try {
75 path = DbConfig.class.getClassLoader().getResource("").toURI().getPath();
76 fis = new FileInputStream(new File(path + ACTIONPATH));
77 prop.load(fis);
78 dc.db_username=prop.getProperty("db_username");
79 dc.db_password=prop.getProperty("db_password");
80 dc.db_name=prop.getProperty("db_name");
81 dc.ip=prop.getProperty("ip");
82 dc.ags_user=prop.getProperty("ags_user");
83 dc.ags_password=prop.getProperty("ags_password");
84 dc.ags_domain=prop.getProperty("ags_domain");
85 dc.ags_host=prop.getProperty("ags_host");
86 dc.ags_servicename=prop.getProperty("ags_servicename");
87 } catch (URISyntaxException e) {
88 e.printStackTrace();
89 } catch (FileNotFoundException e) {
90 e.printStackTrace();
91 } catch (IOException e) {
92 e.printStackTrace();
93 }
94
95 return dc;
96 }
97 }
对应的配置文件如下:
config.properties
#database username
db_username=sde
#database password
db_password=sde
#database server name
db_name=RASA
#database server ip
ip=localhost
#arcgis server username
ags_user=zj
#arcgis server password
ags_password=0.
#arcgis server user domain
ags_domain=zj
#arcgis server host address
ags_host=localhost
#arcgis server feature service name
ags_servicename=map
1 AO
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
//Author: Domenico Ciavarella
//www.studioat.it
namespace road
{
public class ConvertDXFArgs : System.EventArgs
{
bool result = false;
Exception ex = null;
public ConvertDXFArgs()
{
result = true;
}
public ConvertDXFArgs(Exception ex)
{
this.ex = ex;
}
public bool Result
{
get { return result; }
}
public Exception Exception
{
get { return ex; }
}
}
public class ConvertDXF
{
public delegate void OnCompleted(object sender, ConvertDXFArgs e);
public event OnCompleted completed;
struct ColorDXF
{
public byte Red;
public byte Green;
public byte Blue;
public ColorDXF(byte r, byte g, byte b)
{
Red = r;
Green = g;
Blue = b;
}
}
struct TagDXF
{
public const string HEADER = "HEADER";
public const string TABLES = "TABLES";
public const string ENTITIES = "ENTITIES";
public const string BLOCKS = "BLOCKS";
}
string m_Filename = null;
IFeatureLayer m_FeatureLayer = null;
TextWriter m_tw = null;
public ConvertDXF(string filename, IFeatureLayer featureLayer)
{
m_Filename = filename;
m_FeatureLayer = featureLayer;
}
public void Convert()
{
if ((m_FeatureLayer == null) || (string.IsNullOrEmpty(m_Filename)))
{
completed(this, new ConvertDXFArgs(new Exception("FeatureLayer is null or Filename is null")));
}
try
{
m_tw = new StreamWriter(m_Filename);
IEnvelope envelope = m_FeatureLayer.AreaOfInterest;
IFeatureClass featureClass = m_FeatureLayer.FeatureClass;
Header(envelope);
Tables();
Blocks();
Entities();
completed(this, new ConvertDXFArgs());
}
catch (Exception ex)
{
completed(this, new ConvertDXFArgs(ex));
}
finally
{
m_tw.Close();
m_tw.Dispose();
m_tw = null;
}
}
/// <summary>
/// return color nearest Autocad
/// </summary>
/// <param name="intR"></param>
/// <param name="intG"></param>
/// <param name="intB"></param>
/// <returns></returns>
static ColorDXF GetColorDXF(ColorDXF c)
{
IDictionary<byte, string> dct = new Dictionary<byte, string>();
#region Dictionary Color
//item: colore come valore RGB, key: colore equivalent in AutoCAD
dct.Add(0, "0|0|0");
dct.Add(1, "255|0|0");
dct.Add(2, "255|255|0");
dct.Add(3, "0|255|0");
dct.Add(4, "0|255|255");
dct.Add(5, "0|0|255");
dct.Add(6, "255|0|255");
dct.Add(7, "255|255|255");
dct.Add(8, "65|65|65");
dct.Add(9, "128|128|128");
dct.Add(10, "255|0|0");
dct.Add(11, "255|170|170");
dct.Add(12, "189|0|0");
dct.Add(13, "189|126|126");
dct.Add(14, "129|0|0");
dct.Add(15, "129|86|86");
dct.Add(16, "104|0|0");
dct.Add(17, "104|69|69");
dct.Add(18, "79|0|0");
dct.Add(19, "79|53|53");
dct.Add(20, "255|63|0");
dct.Add(21, "255|191|170");
dct.Add(22, "189|46|0");
dct.Add(23, "189|141|126");
dct.Add(24, "129|31|0");
dct.Add(25, "129|96|86");
dct.Add(26, "104|25|0");
dct.Add(27, "104|78|69");
dct.Add(28, "79|19|0");
dct.Add(29, "79|59|53");
dct.Add(30, "255|127|0");
dct.Add(31, "255|212|170");
dct.Add(32, "189|94|0");
dct.Add(33, "189|157|126");
dct.Add(34, "129|64|0");
dct.Add(35, "129|107|86");
dct.Add(36, "104|52|0");
dct.Add(37, "104|86|69");
dct.Add(38, "79|39|0");
dct.Add(39, "79|66|53");
dct.Add(40, "255|191|0");
dct.Add(41, "255|234|170");
dct.Add(42, "189|141|0");
dct.Add(43, "189|173|126");
dct.Add(44, "129|96|0");
dct.Add(45, "129|118|86");
dct.Add(46, "104|78|0");
dct.Add(47, "104|95|69");
dct.Add(48, "79|59|0");
dct.Add(49, "79|73|53");
dct.Add(50, "255|255|0");
dct.Add(51, "255|255|170");
dct.Add(52, "189|189|0");
dct.Add(53, "189|189|126");
dct.Add(54, "129|129|0");
dct.Add(55, "129|129|86");
dct.Add(56, "104|104|0");
dct.Add(57, "104|104|69");
dct.Add(58, "79|79|0");
dct.Add(59, "79|79|53");
dct.Add(60, "191|255|0");
dct.Add(61, "234|255|170");
dct.Add(62, "141|189|0");
dct.Add(63, "173|189|126");
dct.Add(64, "96|129|0");
dct.Add(65, "118|129|86");
dct.Add(66, "78|104|0");
dct.Add(67, "95|104|69");
dct.Add(68, "59|79|0");
dct.Add(69, "73|79|53");
dct.Add(70, "127|255|0");
dct.Add(71, "212|255|170");
dct.Add(72, "94|189|0");
dct.Add(73, "157|189|126");
dct.Add(74, "64|129|0");
dct.Add(75, "107|129|86");
dct.Add(76, "52|104|0");
dct.Add(77, "86|104|69");
dct.Add(78, "39|79|0");
dct.Add(79, "66|79|53");
dct.Add(80, "63|255|0");
dct.Add(81, "191|255|170");
dct.Add(82, "46|189|0");
dct.Add(83, "141|189|126");
dct.Add(84, "31|129|0");
dct.Add(85, "96|129|86");
dct.Add(86, "25|104|0");
dct.Add(87, "78|104|69");
dct.Add(88, "19|79|0");
dct.Add(89, "59|79|53");
dct.Add(90, "0|255|0");
dct.Add(91, "170|255|170");
dct.Add(92, "0|189|0");
dct.Add(93, "126|189|126");
dct.Add(94, "0|129|0");
dct.Add(95, "86|129|86");
dct.Add(96, "0|104|0");
dct.Add(97, "69|104|69");
dct.Add(98, "0|79|0");
dct.Add(99, "53|79|53");
dct.Add(100, "0|255|63");
dct.Add(101, "170|255|191");
dct.Add(102, "0|189|46");
dct.Add(103, "126|189|141");
dct.Add(104, "0|129|31");
dct.Add(105, "86|129|96");
dct.Add(106, "0|104|25");
dct.Add(107, "69|104|78");
dct.Add(108, "0|79|19");
dct.Add(109, "53|79|59");
dct.Add(110, "0|255|127");
dct.Add(111, "170|255|212");
dct.Add(112, "0|189|94");
dct.Add(113, "126|189|157");
dct.Add(114, "0|129|64");
dct.Add(115, "86|129|107");
dct.Add(116, "0|104|52");
dct.Add(117, "69|104|86");
dct.Add(118, "0|79|39");
dct.Add(119, "53|79|66");
dct.Add(120, "0|255|191");
dct.Add(121, "170|255|234");
dct.Add(122, "0|189|141");
dct.Add(123, "126|189|173");
dct.Add(124, "0|129|96");
dct.Add(125, "86|129|118");
dct.Add(126, "0|104|78");
dct.Add(127, "69|104|95");
dct.Add(128, "0|79|59");
dct.Add(129, "53|79|73");
dct.Add(130, "0|255|255");
dct.Add(131, "170|255|255");
dct.Add(132, "0|189|189");
dct.Add(133, "126|189|189");
dct.Add(134, "0|129|129");
dct.Add(135, "86|129|129");
dct.Add(136, "0|104|104");
dct.Add(137, "69|104|104");
dct.Add(138, "0|79|79");
dct.Add(139, "53|79|79");
dct.Add(140, "0|191|255");
dct.Add(141, "170|234|255");
dct.Add(142, "0|141|189");
dct.Add(143, "126|173|189");
dct.Add(144, "0|96|129");
dct.Add(145, "86|118|129");
dct.Add(146, "0|78|104");
dct.Add(147, "69|95|104");
dct.Add(148, "0|59|79");
dct.Add(149, "53|73|79");
dct.Add(150, "0|127|255");
dct.Add(151, "170|212|255");
dct.Add(152, "0|94|189");
dct.Add(153, "126|157|189");
dct.Add(154, "0|64|129");
dct.Add(155, "86|107|129");
dct.Add(156, "0|52|104");
dct.Add(157, "69|86|104");
dct.Add(158, "0|39|79");
dct.Add(159, "53|66|79");
dct.Add(160, "0|63|255");
dct.Add(161, "170|191|255");
dct.Add(162, "0|46|189");
dct.Add(163, "126|141|189");
dct.Add(164, "0|31|129");
dct.Add(165, "86|96|129");
dct.Add(166, "0|25|104");
dct.Add(167, "69|78|104");
dct.Add(168, "0|19|79");
dct.Add(169, "53|59|79");
dct.Add(170, "0|0|255");
dct.Add(171, "170|170|255");
dct.Add(172, "0|0|189");
dct.Add(173, "126|126|189");
dct.Add(174, "0|0|129");
dct.Add(175, "86|86|129");
dct.Add(176, "0|0|104");
dct.Add(177, "69|69|104");
dct.Add(178, "0|0|79");
dct.Add(179, "53|53|79");
dct.Add(180, "63|0|255");
dct.Add(181, "191|170|255");
dct.Add(182, "46|0|189");
dct.Add(183, "141|126|189");
dct.Add(184, "31|0|129");
dct.Add(185, "96|86|129");
dct.Add(186, "25|0|104");
dct.Add(187, "78|69|104");
dct.Add(188, "19|0|79");
dct.Add(189, "59|53|79");
dct.Add(190, "127|0|255");
dct.Add(191, "212|170|255");
dct.Add(192, "94|0|189");
dct.Add(193, "157|126|189");
dct.Add(194, "64|0|129");
dct.Add(195, "107|86|129");
dct.Add(196, "52|0|104");
dct.Add(197, "86|69|104");
dct.Add(198, "39|0|79");
dct.Add(199, "66|53|79");
dct.Add(200, "191|0|255");
dct.Add(201, "234|170|255");
dct.Add(202, "141|0|189");
dct.Add(203, "173|126|189");
dct.Add(204, "96|0|129");
dct.Add(205, "118|86|129");
dct.Add(206, "78|0|104");
dct.Add(207, "95|69|104");
dct.Add(208, "59|0|79");
dct.Add(209, "73|53|79");
dct.Add(210, "255|0|255");
dct.Add(211, "255|170|255");
dct.Add(212, "189|0|189");
dct.Add(213, "189|126|189");
dct.Add(214, "129|0|129");
dct.Add(215, "129|86|129");
dct.Add(216, "104|0|104");
dct.Add(217, "104|69|104");
dct.Add(218, "79|0|79");
dct.Add(219, "79|53|79");
dct.Add(220, "255|0|191");
dct.Add(221, "255|170|234");
dct.Add(222, "189|0|141");
dct.Add(223, "189|126|173");
dct.Add(224, "129|0|96");
dct.Add(225, "129|86|118");
dct.Add(226, "104|0|78");
dct.Add(227, "104|69|95");
dct.Add(228, "79|0|59");
dct.Add(229, "79|53|73");
dct.Add(230, "255|0|127");
dct.Add(231, "255|170|212");
dct.Add(232, "189|0|94");
dct.Add(233, "189|126|157");
dct.Add(234, "129|0|64");
dct.Add(235, "129|86|107");
dct.Add(236, "104|0|52");
dct.Add(237, "104|69|86");
dct.Add(238, "79|0|39");
dct.Add(239, "79|53|66");
dct.Add(240, "255|0|63");
dct.Add(241, "255|170|191");
dct.Add(242, "189|0|46");
dct.Add(243, "189|126|141");
dct.Add(244, "129|0|31");
dct.Add(245, "129|86|96");
dct.Add(246, "104|0|25");
dct.Add(247, "104|69|78");
dct.Add(248, "79|0|19");
dct.Add(249, "79|53|59");
dct.Add(250, "51|51|51");
dct.Add(251, "80|80|80");
dct.Add(252, "105|105|105");
dct.Add(253, "130|130|130");
dct.Add(254, "190|190|190");
dct.Add(255, "255|255|255");
#endregion Dictionary Color
string[] sColorCAD; //tutti possibili colori della collection
byte bGCAD, bRCAD, bBCAD; //valori RGB di tutti colori
byte color = 0;
//color nearest of collection
double dblTemp = Double.MaxValue;
double dblDistance;
foreach (byte idx in dct.Keys)
{
sColorCAD = dct[idx].Split('|');
bRCAD = byte.Parse(sColorCAD[0]);
bGCAD = byte.Parse(sColorCAD[1]);
bBCAD = byte.Parse(sColorCAD[2]);
dblDistance = System.Math.Sqrt((bRCAD - c.Red) ^ 2 + (bGCAD - c.Green) ^ 2 + (bBCAD - c.Blue) ^ 2);
if (dblDistance < dblTemp)
{
dblTemp = dblDistance;
color = idx;
if (dblTemp == 0)
break;
}
}
//color "0" (not defined) -> color "7" (default of AutoCAD)
if (color == 0)
color = 7;
string[] r = dct[color].Split('|');
return new ColorDXF(byte.Parse(r[0]), byte.Parse(r[1]), byte.Parse(r[2]));
}
static IFormatProvider GetFormatProvider()
{
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalSeparator = ".";
return nfi;
}
void BeginSection(string Tag)
{
m_tw.WriteLine(0);
m_tw.WriteLine("SECTION");
m_tw.WriteLine(2);
m_tw.WriteLine(Tag);
}
void EndSection()
{
m_tw.WriteLine(0);
m_tw.WriteLine("ENDSEC");
}
void EOF()
{
m_tw.WriteLine(0);
m_tw.WriteLine("EOF");
}
void Point(IPointCollection pPoints, string LayerName, byte? Color)
{
for (int i = 0; i < pPoints.PointCount; i++)
{
Point(pPoints.get_Point(i), LayerName, Color);
}
}
void Blocks()
{
BeginSection(TagDXF.BLOCKS);
EndSection();
}
void Tables()
{
BeginSection(TagDXF.TABLES);
EndSection();
}
void Point(IPoint pPoint, string LayerName, byte? Color)
{
m_tw.WriteLine(0);
m_tw.WriteLine("POINT");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
m_tw.WriteLine(62);
if (Color != null)
m_tw.WriteLine(Color);
m_tw.WriteLine(10);
m_tw.WriteLine(pPoint.X.ToString(GetFormatProvider()));
m_tw.WriteLine(20);
m_tw.WriteLine(pPoint.Y.ToString(GetFormatProvider()));
m_tw.WriteLine(39); //Thickness
m_tw.WriteLine(3);
}
void Header(IEnvelope extent)
{
IPoint LLExtents = extent.LowerLeft;
IPoint URExtents = extent.UpperRight;
BeginSection(TagDXF.HEADER);
m_tw.WriteLine(9);
m_tw.WriteLine("$EXTMIN");
m_tw.WriteLine(10);
m_tw.WriteLine(LLExtents.X.ToString(GetFormatProvider()));
m_tw.WriteLine(20);
m_tw.WriteLine(LLExtents.Y.ToString(GetFormatProvider()));
m_tw.WriteLine(30);
m_tw.WriteLine(0);
m_tw.WriteLine(9);
m_tw.WriteLine("$EXTMAX");
m_tw.WriteLine(10);
m_tw.WriteLine(URExtents.X.ToString(GetFormatProvider()));
m_tw.WriteLine(20);
m_tw.WriteLine(URExtents.Y.ToString(GetFormatProvider()));
m_tw.WriteLine(30);
m_tw.WriteLine(0);
EndSection();
}
void Polyline(IGeometry pShape, string LayerName, byte? Color)
{
IGeometryCollection geometryCollection;
if (pShape is IGeometryCollection)
geometryCollection = pShape as IGeometryCollection;
else
{
object o = Type.Missing;
geometryCollection = new Polygon() as IGeometryCollection;
geometryCollection.AddGeometry(pShape, ref o, ref o);
}
for (int j = 0; j < geometryCollection.GeometryCount; j++)
{
ISegmentCollection pSegColl = geometryCollection.get_Geometry(j) as ISegmentCollection;
bool bFirstSeg = true;
ICircularArc pCA = null;
ISegment pSeg = null;
for (int i = 0; i < pSegColl.SegmentCount; i++)
{
pSeg = pSegColl.get_Segment(i);
switch (pSeg.GeometryType)
{
case esriGeometryType.esriGeometryLine:
if (bFirstSeg)
{
m_tw.WriteLine(0);
m_tw.WriteLine("POLYLINE");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
m_tw.WriteLine(66);
m_tw.WriteLine(1);
if (Color != null)
{
m_tw.WriteLine(62);
m_tw.WriteLine(Color);
}
m_tw.WriteLine(6);
m_tw.WriteLine("CONTINUOUS");
//from point
m_tw.WriteLine(0);
m_tw.WriteLine("VERTEX");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
m_tw.WriteLine(10);
m_tw.WriteLine(pSeg.FromPoint.X.ToString(GetFormatProvider()));
m_tw.WriteLine(20);
m_tw.WriteLine(pSeg.FromPoint.Y.ToString(GetFormatProvider()));
bFirstSeg = !bFirstSeg;
}
// to point
m_tw.WriteLine(0);
m_tw.WriteLine("VERTEX");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
m_tw.WriteLine(10);
m_tw.WriteLine(pSeg.ToPoint.X.ToString(GetFormatProvider()));
m_tw.WriteLine(20);
m_tw.WriteLine(pSeg.ToPoint.Y.ToString(GetFormatProvider()));
if (i == (pSegColl.SegmentCount - 1))
{
m_tw.WriteLine(0);
m_tw.WriteLine("SEQEND");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
}
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryCircularArc:
if (!bFirstSeg)
{
m_tw.WriteLine(0);
m_tw.WriteLine("SEQEND");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
}
pCA = pSeg as ICircularArc;
m_tw.WriteLine(0);
m_tw.WriteLine("ARC");
m_tw.WriteLine(8);
m_tw.WriteLine(LayerName);
m_tw.WriteLine(10);
m_tw.WriteLine(pCA.CenterPoint.X.ToString(GetFormatProvider()));
m_tw.WriteLine(20);
m_tw.WriteLine(pCA.CenterPoint.Y.ToString(GetFormatProvider()));
m_tw.WriteLine(40);
m_tw.WriteLine(pCA.Radius);
string sFromAngle = (pCA.FromAngle * 180 / Math.PI).ToString(GetFormatProvider());
string sToAngle = (pCA.ToAngle * 180 / Math.PI).ToString(GetFormatProvider());
m_tw.WriteLine(50);
m_tw.WriteLine(pCA.IsCounterClockwise ? sFromAngle : sToAngle);
m_tw.WriteLine(51);
m_tw.WriteLine(pCA.IsCounterClockwise ? sToAngle : sFromAngle);
bFirstSeg = true;
break;
}
}
}
}
void Polygon(IGeometry pShape, string LayerName, byte? Color)
{
IPolygon4 polygon = pShape as IPolygon4;
IGeometryBag exteriorRings = polygon.ExteriorRingBag;
//For each exterior rings find the interior rings associated with it
IEnumGeometry exteriorRingsEnum = exteriorRings as IEnumGeometry;
exteriorRingsEnum.Reset();
IRing currentExteriorRing = exteriorRingsEnum.Next() as IRing;
while (currentExteriorRing != null)
{
Polyline(currentExteriorRing, LayerName, Color);
IGeometryBag interiorRings = polygon.get_InteriorRingBag(currentExteriorRing);
IEnumGeometry interiorRingsEnum = interiorRings as IEnumGeometry;
interiorRingsEnum.Reset();
IRing currentInteriorRing = interiorRingsEnum.Next() as IRing;
while (currentInteriorRing != null)
{
Polyline(currentInteriorRing, LayerName, Color);
currentInteriorRing = interiorRingsEnum.Next() as IRing;
}
currentExteriorRing = exteriorRingsEnum.Next() as IRing;
}
}
void Entities()
{
BeginSection(TagDXF.ENTITIES);
byte? Color = 7;
string LayerName = m_FeatureLayer.Name;
IFeatureClass featureClass = m_FeatureLayer.FeatureClass;
IFeatureCursor featureCursor = featureClass.Search(null, true);
IFeature feature = featureCursor.NextFeature();
IGeometry shape = null;
while (feature != null)
{
shape = feature.ShapeCopy;
//int? OID = feature.HasOID ? feature.OID : new Nullable<int>();
switch (shape.GeometryType)
{
case esriGeometryType.esriGeometryPoint:
Point(shape as IPoint, LayerName, Color);
break;
case esriGeometryType.esriGeometryMultipoint:
Point(shape as IPointCollection, LayerName, Color);
break;
case esriGeometryType.esriGeometryPolyline:
Polyline(shape, LayerName, Color);
break;
case esriGeometryType.esriGeometryPolygon:
Polygon(shape, LayerName, Color);
break;
case esriGeometryType.esriGeometryEnvelope:
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPath:
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny:
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryRing:
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryLine:
Polyline(shape, LayerName, Color);
break;
case esriGeometryType.esriGeometryCircularArc:
case esriGeometryType.esriGeometryBezier3Curve:
case esriGeometryType.esriGeometryEllipticArc:
break;
}
feature = featureCursor.NextFeature();
}
EndSection();
EOF();
}
}
}
包装为icommand,编译成dll,其他地方可直接调用
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using System.IO;
//Author: Domenico Ciavarella
//www.studioat.it
namespace road
{
public sealed class ExportDXF : BaseCommand
{
IHookHelper m_hookHelper = null;
public ExportDXF()
{
}
#region Overriden Class Methods
public override string Tooltip
{
get
{
return "Export DXF";
}
}
public override string Caption
{
get
{
return "Export DXF";
}
}
public override string Message
{
get
{
return "Export DXF";
}
}
public override void OnCreate(object hook)
{
if (hook == null)
return;
try
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.ActiveView == null)
m_hookHelper = null;
}
catch
{
m_hookHelper = null;
}
if (m_hookHelper == null)
base.m_enabled = false;
else
base.m_enabled = true;
}
public override void OnClick()
{
if (null == m_hookHelper)
return;
object hook = null;
if (m_hookHelper.Hook is IToolbarControl2)
{
hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy;
}
else
{
hook = m_hookHelper.Hook;
}
object customProperty = null;
IMapControl3 mapControl = null;
if (hook is IMapControl3)
{
mapControl = (IMapControl3)hook;
customProperty = mapControl.CustomProperty;
}
else
return;
if (null == customProperty || !(customProperty is ILayer))
return;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "DXF File|*.dxf";
saveFileDialog.Title = "Export layer in dxf";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory, ((ILayer)customProperty).Name + ".dxf");
DialogResult dr = saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != string.Empty && dr == DialogResult.OK)
{
if (System.IO.File.Exists(saveFileDialog.FileName))
{
System.IO.File.Delete(saveFileDialog.FileName);
}
ILayer layer = customProperty as ILayer;
ConvertDXF dxf = new ConvertDXF(saveFileDialog.FileName, layer as IFeatureLayer);
dxf.completed += new ConvertDXF.OnCompleted(dxf_completed);
dxf.Convert();
}
}
void dxf_completed(object sender, ConvertDXFArgs e)
{
if (e.Result)
MessageBox.Show("Export success!");
else
MessageBox.Show("Export failed:" + e.Exception.Message);
}
#endregion
}
}
使用:
IMap map = m_mapControl.Map;
ILayer layer = map.get_Layer(0);
m_mapControl.CustomProperty = layer;
ICommand cmd = new ExportDXF();
cmd.OnCreate(m_mapControl.Object);
cmd.OnClick();
因使用AutoCAD版本为2004,2006版本以后才有.net开发包,因此采用ActiveX方式进行开发。
功能为:将几个CAD图层合并到其中一个上,并设置坐标,线和标记的颜色和字体。
CAD对象的层级为
AcadApplication -
AcadDocument -
AcadModelSpace
AcadEntity
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 using AutoCAD;
10
11 namespace road
12 {
13 public class CadMerge
14 {
15 #region members
16
17 private AcadApplication app = null;
18 private AcadDocument docLine = null;
19 private AcadDocument docLineAnno = null;
20 private AcadDocument docPoint = null;
21 private AcadDocument docPointAnno = null;
22
23 private AcadModelSpace msLineAnno = null;
24
25 private string dirPath = "";
26 private string tempPath = "";
27 private string linePath = "";
28 private string pointPath = "";
29 private string pointAnnoPath = "";
30 private string lineAnnoPath = "";
31
32 private ProgressBar m_bar = null;
33
34 #endregion
35
36 public CadMerge(string path, ProgressBar bar)
37 {
38 this.dirPath = path;
39 m_bar = bar;
40 init();
41 }
42
43 //初始化参数
44 private void init()
45 {
46 dirPath = dirPath+"\\";
47 tempPath = dirPath + "temp\\";
48 linePath = tempPath + "line.dwg";
49 lineAnnoPath = dirPath + MainForm.m_resultDwgName;
50 pointPath = tempPath + "point.dwg";
51 pointAnnoPath = tempPath + "point_anno.dwg";
52
53 app = new AcadApplication();
54 m_bar.PerformStep();
55 //app.Application.Visible = true;
56 docLine = app.Documents.Open(linePath, null, null);
57 docLineAnno = app.Documents.Open(lineAnnoPath, null, null);
58 m_bar.PerformStep();
59 docPoint = app.Documents.Open(pointPath, null, null);
60 docPointAnno = app.Documents.Open(pointAnnoPath, null, null);
61 m_bar.PerformStep();
62 }
63
64 //关闭cad文档
65 private void CloseDocuments(AcadApplication app)
66 {
67 AcadDocuments docs = app.Documents;
68 foreach (AcadDocument doc in docs)
69 {
70 if (doc.ReadOnly)
71 {
72 doc.Close(false, null);
73 }
74 else
75 {
76 doc.Close(true, null);
77 }
78 }
79 }
80
81 //合并cad文档
82 public void MergeCADLayers()
83 {
84 msLineAnno = docLineAnno.ModelSpace;
85 AcadModelSpace msLine = docLine.ModelSpace;
86 AcadModelSpace msPoint = docPoint.ModelSpace;
87 AcadModelSpace msPointAnno = docPointAnno.ModelSpace;
88 updateLineAnno();
89 AddLineToLineAnno(msLineAnno, msLine);
90 m_bar.PerformStep();
91 AddPointToLineAnno(msLineAnno, msPoint);
92 m_bar.PerformStep();
93 AddPointAnnoToLineAnno(msLineAnno, msPointAnno);
94 updateColor();
95 app.Update();
96 docLineAnno.Save();
97 CloseDocuments(app);
98 }
99
100 //更新线标记图层
101 private void updateLineAnno()
102 {
103 foreach (AcadEntity entity in msLineAnno)
104 {
105 if (entity is AcadText)
106 {
107 AcadText text = (AcadText)entity;
108 msLineAnno.AddText(text.TextString, getPoint(text.InsertionPoint), text.Height);
109 text.Delete();
110 }
111 }
112 }
113
114 //点的转换,新坐标要求
115 private double[] getPoint(object from)
116 {
117 double[] point = new double[3];
118 point[0] = ((double[])from)[0]-200000;
119 point[1] = ((double[])from)[1]-200000;
120 point[2] = 0;
121
122 return point;
123 }
124
125 /// <summary>更新线和标记的颜色,标记的文字大小
126 /// </summary>
127 private void updateColor()
128 {
129 foreach (AcadEntity entity in msLineAnno)
130 {
131 if (entity is AcadText)
132 {
133 AcadText text = (AcadText)entity;
134 text.color = ACAD_COLOR.acRed;
135 text.Height = 3.5;
136 }
137 else if (entity is AcadLWPolyline)
138 {
139 AcadLWPolyline line = (AcadLWPolyline)entity;
140 line.color = ACAD_COLOR.acBlue;
141 }
142 else if (entity is AcadLine)
143 {
144 AcadLine line = (AcadLine)entity;
145 line.color = ACAD_COLOR.acBlue;
146 }
147 else if (entity is AcadPoint)
148 {
149 AcadPoint point = (AcadPoint)entity;
150 point.color = ACAD_COLOR.acGreen;
151 }
152 else if (entity is AcadSpline)
153 {
154 AcadSpline line = (AcadSpline)entity;
155 line.color = ACAD_COLOR.acBlue;
156 }
157 else if (entity is AcadPolyline)
158 {
159 AcadPolyline line = (AcadPolyline)entity;
160 line.color = ACAD_COLOR.acBlue;
161 }
162 //MessageBox.Show(entity.ObjectName);
163 }
164 }
165
166 /// <summary>将点标记添加到线标记图层
167 /// </summary>
168 private void AddPointAnnoToLineAnno(AcadModelSpace msLineAnno, AcadModelSpace msPointAnno)
169 {
170 foreach (AcadEntity entity in msPointAnno)
171 {
172 if (entity is AcadText)
173 {
174 AcadText text = (AcadText)entity;
175 msLineAnno.AddText(text.TextString,getPoint(text.InsertionPoint),text.Height);
176 }
177 }
178 }
179
180 /// <summary>将线添加到线标记图层
181 /// </summary>
182 private void AddLineToLineAnno(AcadModelSpace msLineAnno,AcadModelSpace msLine)
183 {
184 foreach (AcadEntity entity in msLine)
185 {
186 if (entity is AcadLWPolyline)
187 {
188 AcadLWPolyline line = (AcadLWPolyline)entity;
189 double[] fromPoints = (double[])line.Coordinates;
190 int count = fromPoints.Length;
191 double newCount = count * 1.5;
192 int newCounti = (int)newCount;
193 double[] points = new double[newCounti];
194
195 //原entity中double数组为x,y,x,y
196 //新Polyline要求点为x,y,z,x,y,z,此处z=0
197 for (int i = 0; i < count; i++)
198 {
199
200 int index = i / 2;
201 int j = index * 3;
202
203 if (i % 2 == 0)
204 {
205 points[j] = fromPoints[i]-200000;
206 points[j + 2] = 0;
207 }
208 else
209 {
210 points[j + 1] = fromPoints[i]-200000;
211 }
212 }
213 msLineAnno.AddPolyline(points);
214 }
215 else if (entity is AcadLine)
216 {
217 AcadLine line = (AcadLine)entity;
218 double[] startPoint = new double[3];
219 double[] endPoint = new double[3];
220 startPoint[0] = ((double[])line.StartPoint)[0] - 200000;
221 startPoint[1] = ((double[])line.StartPoint)[1] - 200000;
222 startPoint[2] = 0;
223 endPoint[0] = ((double[])line.EndPoint)[0] - 200000;
224 endPoint[1] = ((double[])line.EndPoint)[1] - 200000;
225 endPoint[2] = 0;
226 msLineAnno.AddLine(startPoint, endPoint);
227 }
228 }
229 }
230
231 /// <summary>将点添加到线标记图层
232 /// </summary>
233 private void AddPointToLineAnno(AcadModelSpace msLineAnno, AcadModelSpace msPoint)
234 {
235 foreach (AcadEntity entity in msPoint)
236 {
237 if (entity is AcadPoint)
238 {
239 AcadPoint point = (AcadPoint)entity;
240 double[] fromPoint = (double[])point.Coordinates;
241 double[] topoint = new double[3];
242 topoint[0] = fromPoint[0]-200000;
243 topoint[1] = fromPoint[1] - 200000;
244 topoint[2] = 0;
245 msLineAnno.AddPoint(topoint); ;
246 }
247 }
248 }
249 }
250 }
3 修改字体
AcadTextStyle style=docLineAnno.TextStyles.Add("宋体");
//style.fontFile="C:/WINDOWS/Fonts/simsun.ttc"; // 程序在其他地方使用时,生成的cad文件打开时,可能会需要选择字体
style.SetFont("宋体",false,false,0,0); //cad文件打开时,不需要选择字体
//style.fontFile="C:/Programe Files/AutoCAD 2004/gbcbig.shx"; // 程序在其他地方使用时,生成的cad文件打开时,可能会需要选择字体
docLineAnno.ActiveTextStyle=style;
foreach(AcadEntity entity in msLineAnno)
{
if(entity is AcadText)
{
AcadText text=(AcadText)entity;
text.StyleName="宋体";
text.color=ACAD_COLOR.acByLayer;
text.Height=3.5;
text.ScaleFactor=0.75;
}
}
参考:
1 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
2 Workbook workbook = app.Workbooks.Add(true);
3 Worksheet sheet = (Worksheet)workbook.Worksheets["sheet1"];
4 sheet = (Worksheet)workbook.Worksheets.Add(Type.Missing, workbook.Worksheets[1], 1, Type.Missing);
5 sheet.Activate();
6
7 //填充数据
8 sheet.Cells[1, 1] = "序号";
9 sheet.Cells[1, 2] = "道路编码";
10 sheet.Cells[1, 3] = "起始节点";
11 sheet.Cells[1, 4] = "结束节点";
12 for (int i = 0; i < relations.Count; i++)
13 {
14 Relation r = relations[i];
15 sheet.Cells[2 + i, 1] = r.index;
16 sheet.Cells[2 + i, 2] = r.roadNo;
17 sheet.Cells[2 + i, 3] = r.fromPointNo;
18 sheet.Cells[2 + i, 4] = r.toPointNo;
19 }
20
21 //自动调整列宽
22 sheet.Columns.AutoFit();
23
24 //单元格添加边框
25 Range range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[relations.Count+1, 4]);
26 range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
27 range.Borders.LineStyle = 1;
28
29 //第一行加粗并居中显示
30 Range rangeHead = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1,4]);
31 rangeHead.HorizontalAlignment = XlHAlign.xlHAlignCenter;
32 rangeHead.Font.Bold = true;
33
34 app.Visible = false;
35 app.DisplayAlerts = false;
36 workbook.Close(true, filePath, null);
37 object missing = System.Reflection.Missing.Value;
38 sheet = null;
39 workbook = null;
40 app.Quit();
41 System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
42 app = null;
43 GC.Collect();
简单项目中可使用ini做为配置文件,操作类如下。
1 public class INIClass
2 {
3 public string inipath;
4 [System.Runtime.InteropServices.DllImport("kernel32")]
5 private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
6 [System.Runtime.InteropServices.DllImport("kernel32")]
7 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
8 /// <summary>
9 /// 构造方法
10 /// </summary>
11 /// <param name="INIPath">文件路径</param>
12 public INIClass(string INIPath)
13 {
14 inipath = INIPath;
15 }
16 /// <summary>
17 /// 写入INI文件
18 /// </summary>
19 /// <param name="Section">项目名称(如 [TypeName] )</param>
20 /// <param name="Key">键</param>
21 /// <param name="Value">值</param>
22 public void IniWriteValue(string Section, string Key, string Value)
23 {
24 WritePrivateProfileString(Section, Key, Value, this.inipath);
25 }
26 /// <summary>
27 /// 读出INI文件
28 /// </summary>
29 /// <param name="Section">项目名称(如 [TypeName] )</param>
30 /// <param name="Key">键</param>
31 public string IniReadValue(string Section, string Key)
32 {
33 StringBuilder temp = new StringBuilder(500);
34 int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
35 return temp.ToString();
36 }
37 /// <summary>
38 /// 验证文件是否存在
39 /// </summary>
40 /// <returns>布尔值</returns>
41 public bool ExistINIFile()
42 {
43 return File.Exists(inipath);
44 }
45 }
使用方法为:
INIClass ini=new INIClass(System.Windows.Forms.Application.StartupPath + "\\config.ini");
m_parentDirPath = ini.IniReadValue("road", "dir").Trim(); //读取
ini.IniWriteValue("road", "dir", "d:\\aa"); //写入
对应ini文件内容为:
[road]
dir=D:\lzx\data
参考:
1 判断文件夹是否存在
1 /// <summary>文件夹是否存在
2 /// </summary>
3 /// <param name="dir"></param>
4 /// <returns></returns>
5 public static Boolean isFolderExist(string dir)
6 {
7 if (System.IO.Directory.Exists(dir))
8 return true;
9 return false;
10 }
2 判断文件夹是否为空
1 /// <summary>文件夹是否为空
2 /// </summary>
3 /// <param name="dir"></param>
4 /// <returns></returns>
5 public static Boolean isFolderEmpty(string dir)
6 {
7 if (!isFolderExist(dir)) return false;
8
9 System.IO.DirectoryInfo di = new DirectoryInfo(dir);
10 if (di.GetFiles().Length + di.GetDirectories().Length == 0)
11 return true;
12 return false;
13 }
3 创建文件夹
1 /// <summary>创建文件夹
2 /// </summary>
3 /// <param name="dir"></param>
4 /// <returns></returns>
5 public static void createFolder(string dir)
6 {
7 if (!isFolderExist(dir))
8 System.IO.Directory.CreateDirectory(dir);
9 }
4 判断文件名是否合法
1 /// <summary>判断文件名/文件夹名是否合法
2 /// </summary>
3 /// <param name="fileName"></param>
4 /// <returns></returns>
5 public static bool IsValidFileName(string fileName)
6 {
7 bool isValid = true;
8 string errChar = "\\/:*?\"<>|"; //
9 if (string.IsNullOrEmpty(fileName))
10 {
11 isValid = false;
12 }
13 else
14 {
15 for (int i = 0; i < errChar.Length; i++)
16 {
17 if (fileName.Contains(errChar[i].ToString()))
18 {
19 isValid = false;
20 break;
21 }
22 }
23 }
24 return isValid;
25 }
5 删除文件夹
1 /// <summary>删除文件夹及其内容
2 /// </summary>
3 /// <param name="from">原文件夹地址</param>
4 public static void deleteFolder(string from)
5 {
6 if (!Directory.Exists(from))
7 return;
8
9 foreach (string sub in Directory.GetDirectories(from))
10 deleteFolder(sub + "\\");
11
12 foreach (string file in Directory.GetFiles(from))
13 File.Delete(file);
14
15 }
6 拷贝文件夹
1 /// <summary>拷贝文件夹及其内容
2 /// </summary>
3 /// <param name="from">原文件夹地址</param>
4 /// <param name="to">目标文件夹地址</param>
5 public static void copyFolder(string from, string to)
6 {
7 if (!Directory.Exists(to))
8 Directory.CreateDirectory(to);
9
10 foreach (string sub in Directory.GetDirectories(from))
11 copyFolder(sub + "\\", to +"\\"+ System.IO.Path.GetFileName(sub));
12
13 foreach (string file in Directory.GetFiles(from))
14 File.Copy(file, to + "\\" + System.IO.Path.GetFileName(file), true);
15
16 }
7 打开指定文件夹
1 /// <summary>打开指定文件夹
2 /// </summary>
3 /// <param name="dir"></param>
4 public static void openFolderInWindow(string dir)
5 {
6 System.Diagnostics.Process.Start("explorer.exe", dir);
7 }

