您可以设置Windows使用Word或其他应用程序打开.doc文件。我如何创建这样一个c#应用程序,它可以处理,如果为了例子我打开一个.txt文件与该应用程序?所以计划是:这里有一个information.kkk这个文件是一个文本文件,里面有一个数字。我想让我的c#应用程序(Visual Studio 2010)接收该数字,如果文件被它打开。用我的c#应用程序打开特定文件 - 就像使用Word打开.doc文件
回答
如果你打开一个ddd.txt与您的应用程序(exe文件),那么字符串[] Args将有两个项目:程序的路径本身和ddd.txt路径。以下示例代码显示如何将ddd.txt文件放入文本框的Form1。非常感谢大家的帮助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public static class Environment
{
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
string filePath = args[0];
for (int i = 0; i <= args.Length - 1; i++)
{
if (args[i].EndsWith(".exe") == false)
{
textBox1.Text = System.IO.File.ReadAllText(args[i],
Encoding.Default);
}
}
}
private void Application_Startup(object sender, StartupEventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
string filePath = args[0];
}
}
public sealed class StartupEventArgs : EventArgs
{
}
}
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".kkk";
dlg.Filter = "KKK documents (.kkk)|*.kkk";
在控制台应用程序中使用main函数中的args参数。第一个参数是打开文件的路径。
例如:
class Program
{
static void Main(string[] args)
{
var filePath = args[0];
//...
}
}
在WPF应用程序中使用Application_Startup事件:
private void Application_Startup(object sender, StartupEventArgs e)
{
var filePath = e.Args[0];
//...
}
或者使用环境类 - 在你的.NET应用程序的任何地方:
string[] args = Environment.GetCommandLineArgs();
string filePath = args[0];
如果我使用WindowsFormsApplication,该怎么做?它不识别参数部分。 ![image](http://weisza.uw.hu/fajlok/args.png)。 – weiszam
@weiszam:使用Enviroment类(编辑答案)或将参数“string [] args”添加到Main方法(Program.cs) - static void Main(string [] args)。 [示例链接](http://www.blackwasp.co.uk/WindowsFormsStartParams.aspx) – mveith
它现在看起来很完美。我将在下面粘贴一个excample应用程序的代码。非常感谢。 – weiszam
- 1. iOS - 用特定程序打开文件
- 2. C#程序打开Word应用程序但不是特定的Word文档
- 3. 打开使用特定应用程序的MP3文件java
- 4. 无法使用android应用程序打开word文件
- 5. Excel宏 - 打开特定的word文件
- 6. Android - 用另一个特定的应用程序打开文件
- 7. 用c打开word文档#
- 8. 用应用程序打开文件
- 9. 使用打开的xml文件格式API打开文字2003(doc)文件API
- 10. 用C#打开我的应用程序的文件?
- 11. 用自己的C#应用程序打开自定义文件
- 12. 使用“打开方式”不会在我的应用程序中打开文件
- 13. iOS:用UIDocumentInteractionController打开文件,并知道应用程序打开文件
- 14. 如何打开我的C++应用程序内部的文件
- 15. 确定谁使用C#打开文件
- 16. 在iphone应用程序中使用objective-c打开.indd文件?
- 17. 使用Python使用默认应用程序打开文件,然后在文件打开后打印?
- 18. 想要在应用程序中打开Word,Excel和PowerPoint文件
- 19. 按钮,打开应用程序路径+特定文件夹
- 20. 从Android应用程序中打开特定文件
- 21. 打开一个在线应用程序来打开txt文件
- 22. C打开文件:打开文件
- 23. 如何打开SmbFile,就像我们做蒙山文件我们使用Desktop.getDesktop()。打开(新文件(”“))
- 24. 用C#打开.sqlite文件#
- 25. 打开.dat文件用C
- 26. 用C打开文件#
- 27. 打开文档打开我的应用程序的旧版本
- 28. 在C++中使用ofstream打开现有的.doc文件
- 29. 如何在桌面中使用特定程序打开文件
- 30. 使用rake文件在Mac应用程序中打开文档
在[SO here] [1]中有一个较老的答案,似乎涵盖了您的要求。 [1]:http://stackoverflow.com/questions/69761/how-to-associate-a-file-extension-to-the-current-executable-in-c-sharp – renick
同意我正准备粘贴那个,。,。你甜菜我! – FlavorScape
http://stackoverflow.com/questions/2681878/associate-file-extension-with-application这似乎是一个更清洁的方法tho – Machinarius