[英]accept multi line string in Process.Start
我正在從目錄位置解析路徑:
假設InitialPath = @"C:\\Users\\username\\Documents\\Visual Studio 2015\\Projects"
我有一個循環:
var list = new List<string>();
foreach(var folder in Directory.GetDirectories(InitialPath) {
var folder = Path.GetFileName(folder);
var file = Path.GetFileName(Directory.GetFiles(folder, "*.sln").Single());
list.Add(InitialPath + "\\" + folder + "\\" + file); //would then result something like "C:\Users\username\Documents\Visual Studio 2015\Projects\Folder1\Project1inFolder1.sln"
}
如果我嘗試從list
路徑,並將其分配給richbox
作為其.text
值,它將返回單行文本。
但是當我在MessageBox
上顯示它時,該字符串被分成兩行,如下所示:
我需要強迫它不要分成幾行。 我的意思是,無論字符串的長度如何,我都需要它是單行字符串,因為Process.Start()
不會接受該字符串,因為它會被切成幾行。 請參閱以下參考:
PS: sorry for not being able to explain my question eligibly, english is not my natural language
以防萬一,這是我的代碼段:
using MaterialSkin;
using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Principal;
namespace The_Projects {
public partial class MainForm : MaterialForm {
public MainForm() {
InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
public class DirectoryInformation {
private string _FolderName;
private string _Solution;
private DateTime _Created;
private DateTime _Accessed;
private DateTime _Modified;
private string _SecIdentity;
private string _NTAccount;
private double _FileSize;
private int _FileCount;
public string FolderName {
get { return _FolderName; }
set { _FolderName = value; }
}
public string Solution {
get { return _Solution; }
set { _Solution = value; }
}
public DateTime Created {
get { return _Created; }
set { _Created = value; }
}
public DateTime Accessed {
get { return _Accessed; }
set { _Accessed = value; }
}
public DateTime Modified {
get { return _Modified; }
set { _Modified = value; }
}
public string SecIdentity {
get { return _SecIdentity; }
set { _SecIdentity = value; }
}
public string NTAccount {
get { return _NTAccount; }
set { _NTAccount = value; }
}
public double FileSize {
get { return _FileSize; }
set { _FileSize = value; }
}
public int FileCount {
get { return _FileCount; }
set { _FileCount = value; }
}
}
public string InitialPath = @"X:\_\Document\Visual Studio 2015\Projects\";
public string FolderPath = string.Empty;
public string Solution = string.Empty;
private void MainForm_Load(object sender, EventArgs e) {
var projectList = new List<DirectoryInformation>();
foreach(var dirs in Directory.GetDirectories(InitialPath)) {
var ac = File.GetAccessControl(dirs);
var di = new DirectoryInfo(dirs);
var dirInf = new DirectoryInformation() {
FolderName = Path.GetFileName(dirs),
Solution = Path.GetFileName(Directory.GetFiles(dirs, "*.sln").Single()),
Created = Directory.GetCreationTime(dirs),
Accessed = Directory.GetLastAccessTime(dirs),
Modified = Directory.GetLastWriteTime(dirs),
SecIdentity = ac.GetOwner(typeof(SecurityIdentifier)).ToString(),
NTAccount = ac.GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount)).ToString(),
FileSize = (double) di.EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(x => x.Length) / 1024000,
FileCount = Directory.GetFiles(dirs, "*.*", SearchOption.AllDirectories).Count()
};
projectList.Add(dirInf);
}
lstProjectList.DataSource = projectList;
lstProjectList.DisplayMember = "FolderName";
}
private void lstProjectList_SelectedIndexChanged(object sender, EventArgs e) {
var project = lstProjectList.SelectedValue as DirectoryInformation;
lblFolder.Text = project.FolderName;
lblCreated.Text = project.Created.ToString();
lblAccess.Text = project.Accessed.ToString();
lblModified.Text = project.Modified.ToString();
lblIdentifier.Text = project.SecIdentity;
lblOwner.Text = project.NTAccount;
lblSize.Text = project.FileSize.ToString("F2") + " MB";
lblCount.Text = project.FileCount.ToString();
FolderPath = InitialPath + project.FolderName;
Solution = FolderPath + "\\" + project.Solution;
}
private void btnOpenProject_Click(object sender, EventArgs e) {
Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe", Solution);
//Clipboard.SetText(Solution);
}
private void btnOpenFolder_Click(object sender, EventArgs e) {
Process.Start("explorer.exe", FolderPath);
}
}
}
這就是您的MessageBox包裝文字的方式。 您在這里有兩個選擇:
編輯:
更改此:
Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe", Solution);
至
Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe", "\"" + Solution + "\"");
這里發生的是Process.Start
方法的第二個參數被視為第一個參數給定的可執行文件的參數。 所以process.start所做的相當於打開命令提示符並鍵入以下內容(但不完全相同):
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe" X:\_\Document\Visual Studio 2015\.....
並且命令提示符將(空格)視為參數分隔符,因此將X:\\_\\Document\\Visual
視為一個參數,將Studio
視為下一個,依此類推。 當在字符串周圍使用"\\""
時,您將告訴Process.Start,整個內容(包括空格)是單個參數。
您的樣本中有一些錯誤,對我來說這有效,並且我獲得了所有* .sln文件
//this is just to show that you can get short file name if you need FileInfo
var list = new Dictionary<string, string>();
var files = Directory.GetFiles(InitialPath, "*.sln", SearchOption.AllDirectories);
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
list.Add(fileInfo.Name, file);
}
Process.Start(list.FirstOrDefault().Value);
在我的電腦上開始沒有問題。 但是,如果您想讓devenv.exe以打開解決方案的方式開始,您可以這樣做
Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe", @"devenv/""" + fullFilePath + @"""");
您需要在參數devenv /中使用命令,並且必須將路徑括在雙引號(“ fullFilePath”)中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.