compact framework windows mobile wm c#代码 创建快捷方式

已经2018年了,windows mobile已经宣布不维护狠多年了,不要问我为什么还在开发windows mobile的程序,我也不想。公司有一批手持扫描枪设备依然是windows mobile的程序,依然有需求,总不能全部淘汰换成android的吧,新采购的是android的,老采购的还是windows mobile的,还有需求在提,没办法。所以。。。。。

资料是真少得可怜。以下是创建软件快捷方式的代码。。。。

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
public class BLLCreate
{

    //调用方法
    // SHCreateShortcut(@"\Windows\StartUp\" + GetApplicationName() + ".lnk","\"" + GetApplicationFullName() + "\"");
    // myCreateShortCut(@"\Windows\StartUp\" + GetApplicationName() + ".lnk","",GetApplicationFullName());

    // 获取进程名
    public static string GetApplicationName()
    {
        return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
    }

    // 获取进程完全路径名
    public static string GetApplicationFullName()
    {
        return System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
    }


    #region 引用
    [DllImport("coredll.dll", EntryPoint = "SHCreateShortcut")]
    public static extern bool SHCreateShortcut(string shortcut, string target);
    #endregion

    /// <summary>
    /// 创建进程快捷方式
    /// 说明: 需要注意该函数和系统提供API在target参数输入的不同。如果target中含有空格符,
    /// 那么需要在路径外使用2个引号""将整个路径个包含。
    /// </summary>
    /// <param name="shortcut">快捷方式路径</param>
    /// <param name="arguments">参数</param>
    /// <param name="target">需要被创建快捷方式的文件</param>
    /// <returns>true or false</returns>
    public static bool myCreateShortCut(string shortcut, string arguments, string target)
    {
        FileStream fs = null;
        try
        {
            bool bQuoted = false;
            target = target.Trim();
            // 检查字符串中是否还有空格
            if (target.IndexOf(' ') > -1)
                bQuoted = true;

            int len = target.Length;
            string link = "";
            // 有空格,则在路径前后添加引号
            if (bQuoted)
                link = "\"" + target + "\"";

            // 判断参数是否为空
            if (!string.IsNullOrEmpty(arguments))
            {
                link += (" " + arguments);
                // 记得要加上路径和参数中间的空格
                len += (arguments.Length + 1);
            }

            // 写入信息


            fs = new FileStream(shortcut, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            if (File.Exists(shortcut))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(len.ToString() + "#" + link);
                    sw.Close();
                    fs.Close();
                    return true;
                }
            }

            fs.Close();
            return false;
        }
        catch
        {
            fs.Close();
            return false;
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/Jerseyblog/p/9071598.html