我如何设置文件名,SQL扩展名为C# [英] How do I set the file name which , SQL extension C#

查看:59
本文介绍了我如何设置文件名,SQL扩展名为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在richedTextBox中有数据我正在将数据写入文件。并自动创建文件。但是Created文件将被硬化文件名我希望它是我在Textbox1.Text中给出的名称



我作为学生输入了TextBox1



String st = textbox1.text.toString();



 string savefilepath = Path.Combine (Directory.GetParent(System.IO.Directory.GetCurrentDirectory())。Parent.FullName,demo.sql); 





我尝试过:



Quote:

假设我输入textbox1.text作为'student',那么该文件应该是student.sql扩展名我将学生作为名称传递给文件filename.sql

解决方案

首先,为什么你要在.Text属性上调用.ToString(),这已经回归了一个字符串!?



接下来,CurrentDirectory不应该用于任何事情,因为它可以在你不知道的情况下改变。例如,显示一个OpenFileDialog,您只需通过在该对话框中导航文件系统,让用户直接更改当前目录。他们甚至不必选择要打开的文件!这不是当前目录改变的唯一方式,只是它的一个例子。



CurrentDirectory的另一个问题是它可以指向一个文件夹您的应用程序的用户无权创建/写入文件,例如在ProgramFiles下。



无论如何,您至少要构建一个完全限定的路径一个文件,这是一件好事。您在单行代码中执行此操作的方式可能会导致问题。将这些操作分解为可调试代码块。

  string  filename = textbox1.Text; 
if (filename.Length == 0
{
// 文本框中没有任何内容,因此未指定文件名。
// 您可能想告诉用户。
}

// 路径是否已有文件扩展名?
if (!Path.HasExtension(filename))
{
// 不。我们将附加我们自己的。
filename + = 。sql ;
}

// 以众所周知的路径开头。
string saveFilePath = Environment.GetFolderPath(SpecialFolder.CommonDocuments);

saveFilePath = Path.Combine(saveFilePath,filename);



通过分解构建路径的操作,您可以单步执行代码,只需将鼠标悬停在鼠标上即可查看每个操作所处理的数据单步执行代码时调试器中的变量。


 String st = textbox1.text.toString(); 

string savefilepath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory())。Parent.FullName,st +。sql);


I have Data in richedTextBox i am writing data to a file. And the file is created automatically. But the Created file will be harded file name i want it to be the name which i give in the Textbox1.Text

I entered TextBox1 as student

String st = textbox1.text.toString();

string savefilepath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName, "demo.sql");



What I have tried:

Quote:

Suppose i enter textbox1.text as 'student' then the file should be craeted with student.sql extension i am passing student as name to the file as filename.sql

解决方案

First, why on earth are you calling .ToString() on a .Text property, WHICH ALREADY RETURNS A STRING!?

Next, The CurrentDirectory shouldn't be used for anything as it can change without you knowing it. For example, show an OpenFileDialog and you're letting the user directly change the "current directory" just by navigating the file system in that dialog. They don't even have to select a file to open! That's not the only way "current directory" gets changed, just one example of how it can.

Another problem with CurrentDirectory is that it can point to a folder where the user of your application has no permissions to create/write a file, such as under ProgramFiles.

In any case, you're at least build a fully qualified path to a file, and that's a good thing. The way you're doing it, in a single line of code can lead to problems. Break those operations out into a block of debuggable code.

string filename = textbox1.Text;
if (filename.Length == 0)
{
    // There's nothing in the textbox, so no filename was specified.
    // You might want to tell the user that.
}

// Does the path already have a file extension?
if (!Path.HasExtension(filename))
{
    // Nope. We'll append our own.
    filename += ".sql";
}

// Start with a "well-known" path.
string saveFilePath = Environment.GetFolderPath(SpecialFolder.CommonDocuments);

saveFilePath = Path.Combine(saveFilePath, filename);


By breaking out the operations that build the path, you can step through the code to see the data each operation is dealing with just by hovering the mouse over the variable in the debugger when stepping through the code.


String st = textbox1.text.toString();

string savefilepath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName, st + ".sql");


这篇关于我如何设置文件名,SQL扩展名为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆