我已将sample.txt(它只包含一行"aaaa")文件嵌入到项目的资源中,就像在这个答案中一样.当我试图像这样读它:
string s = File.ReadAllText(global::ConsoleApplication.Properties.Resources.sample);
Run Code Online (Sandbox Code Playgroud)
我收到System.IO.FileNotFoundException'异常.附加信息:找不到文件'd:\ Work\Projects\MyTests\ConsoleApplication\ConsoleApplication\bin\Debug\aaaa'.
所以看起来它试图从我的资源文件中取出文件名而不是读取这个文件.为什么会这样?我怎样才能让它读取sample.txt
尝试@Ryios的解决方案并获得Argument null异常
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication.Resources.sample.txt"))
{
TextReader tr = new StreamReader(stream);
string fileContents = tr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
该文件位于d:\ Work\Projects\MyTests\ConsoleApplication\ConsoleApplication\Resources\sample.txt
ps解决了.我必须在sample.txt属性中设置Build Action - embed资源
我可以翻译一个文件。但是我不明白如何翻译整个目录。从文档:
To translate an entire project from one directory tree to another use:
$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
Run Code Online (Sandbox Code Playgroud)
能给我一个具体的例子吗?我有python2文件D:\Dir1,想在中获取新文件D:\Dir2。我现在在命令提示符下Dir1键入c:\Users\........\Python36\Tools\scripts\2to3.py -w
那么我应该使用什么命令?
我有元组列表,我希望元组在索引处具有最小值1.例如,如果我的列表如下:
a =[('a', 2), ('ee', 3), ('mm', 4), ('x', 1)]
Run Code Online (Sandbox Code Playgroud)
我想要返回的元组('x', 1).
目前我正在使用sorted函数来获得这样的结果:
min=sorted(a, key=lambda t: t[1])[0]
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?也许有min功能?
我安装了Visual Studio Enterprise2017。找不到在其中添加Python的方法。因此,我不得不使用Python安装Visual Studio Preview 2017(因为他们在这里建议)。所以现在我有2个VS 2017安装,效果不好。有什么方法可以将python支持安装到VS 2017(不是预览版)?
我有2 arrays个相同的长度,我需要计算他们的位置有多少包含相同的元素.我做了这个功能,但我觉得可以在不创建的情况下完成tuple.有没有更广泛和简单的方法来做到这一点?
static int GetCoincidence(int[] a, int[] b)
{
return a.Zip(b, Tuple.Create).Where(x => x.Item1 == x.Item2).Select(x => 1).Sum();
}
Run Code Online (Sandbox Code Playgroud) 我有一些字符串:"d:\ tmp\abc_list.csv""d:\ tmp\xyzx_list.csv""d:\ tmp\qwert_list.csv"我需要取文件名的第一部分:abx,xyzc,qwert.我现在这样做:
string name = filename.Substring(filename.LastIndexOf('\\') + 1 , filename.IndexOf('_') - filename.LastIndexOf('\\') - 1);
Run Code Online (Sandbox Code Playgroud)
我觉得应该有更容易和更好的方式来做到这一点.它是什么?
有没有办法自动将所有(或特定列)输入大写到WPF Datagrid.当我输入datagrid的单元格并按Enter键时,我需要单元格中的所有文本都成为大写.
在我的应用程序同时使用OpenFileDialog和FolderBrowserDialog按钮单击处理程序:
var fileDialog = new System.Windows.Forms.OpenFileDialog();
var folderDialog = new System.Windows.Forms.FolderBrowserDialog();
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当调用OpenFileDialog它从上次选择文件的文件夹中的资源管理器中启动时.但FolderBrowserDialog无论上次选择哪个文件夹,每次都在浏览器中打开MyComputer.如何为"FolderBrowserDialog"获得相同的行为(记住上次选择的文件夹)?
'OpenFileDialog'存储最后选择文件的文件夹也很有趣?Windows是否为每个应用程序存储它?
I need to create a class similar to this:
class GenericClass<T>
{
public T[] Arr {get; }
public GenericClass(int n)
{
Arr = new T[n];
for (int i = 0; i < n; i++)
{
Arr[i] = null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
But there is a compiler error:
CS0403 Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead.
I don't want to use default(T), because it could be the same …
我有一个文本框,其中有很多行文本,它的更新如下:
public void UpdateMessageBox(TextBox textBox, string text)
{
textBox.SelectionStart = 0;
textBox.SelectionLength = 0;
textBox.SelectedText = String.Format("{0:HH:mm:ss }", DateTime.Now) + text + "\n";
textBox.ScrollToHome();
}
Run Code Online (Sandbox Code Playgroud)
现在我需要立即从单击鼠标中键的行中获取文本,而不是先通过左键单击选择行。
private void textBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
{
e.MouseDevice.GetPosition(textBox) //what next?
}
}
Run Code Online (Sandbox Code Playgroud)
如何从鼠标位置获取文本框行及其文本?
c# ×6
python ×3
.net ×2
wpf ×2
coordinates ×1
datagrid ×1
enumerable ×1
generics ×1
lambda ×1
linq ×1
list ×1
min ×1
mouseevent ×1
null ×1
python-2to3 ×1
string ×1
textbox ×1
where ×1