如何在C#中打开文件 [英] How to open file in C#

查看:94
本文介绍了如何在C#中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用字符串值打开文件



我尝试过:



< pre> string c = string.Empty; 
string s = string.Empty;
string ex = string.Empty;





例如c =sg,s =g,ex = FA1



然后如何链接c,s,ex



 xxx = @C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ,ex.xlsx in c drive 

解决方案

请自行尝试一下: c#路径合并 - Google搜索 [ ^ ]


非常感谢Richart

i尝试

 string path1 =C:\\how \\+ c +\\+ s +\\+ ex +。xlsx; 
xxx = path1;


我用它来构建文件路径。它的用法如下:

 var pathtest =。xslx。GetFilePath(C:,how,c,s,ex); 

使用System; 
使用System.IO;
使用System.Text;

namespace YourNameSpace
{
public static class FileExtensions
{
private static StringBuilder sb = new StringBuilder();

private static char pathdelimiter ='\\';

public static string GetFilePath(this string extension,params string [] pathElements)
{
sb.Clear();

string path = null;

int last = pathElements.Length - 1;

for(int i = 0; i< last; i ++)
{
sb.Append(pathElements [i]);
sb.Append(pathdelimiter);
}

sb.Append(pathElements [last]);
sb.Append(扩展名);

try
{
path = Path.GetFullPath(sb.ToString());
}
catch(例外e)
{
开关(e.GetType()。姓名)
{
caseNotSupportedException:
抛出新的ArgumentException(@扩展名或路径元素滥用冒号,e.InnerException);

caseArgumentNullException:
抛出新的ArgumentNullException(@扩展名或路径元素为空,e.InnerException);

caseInvalidCastException:
抛出新的ArgumentException(@扩展名或路径元素包含无效字符,e.InnerException);

caseSecurityException:
抛出新的ArgumentException(@无法访问该位置,e.InnerException);

casePathTooLongException:
抛出新的ArgumentException(@路径太长,e.InnerException);
}
}

返回路径;
}
}
}


how to open file with string values

What I have tried:

<pre>string c = string.Empty;
        string s = string.Empty;
        string ex = string.Empty;



for example c="sg" ,s ="g",ex="FA1"

then how to link c,s,ex

xxx = @"C:\how\c\s\ex.xlsx";


the above one shows error because it opens how,c,c,ex.xlsx in c drive

解决方案

Please try a little research for yourself: c# path combine - Google Search[^]


thanks you so much Richart
i tried

 string path1 = "C:\\how\\" +c + "\\" + s + "\\" + ex + ".xlsx";
xxx = path1;


I use this for building file-paths. It's used like this:

var pathtest = ".xslx".GetFilePath("C:", "how", "c", "s", "ex");

using System;
using System.IO;
using System.Text;

namespace YourNameSpace
{
    public static class FileExtensions
    {
        private static StringBuilder sb = new StringBuilder();

        private static char pathdelimiter = '\\';

        public static string GetFilePath(this string extension, params string[] pathElements)
        {
            sb.Clear();

            string path = null;

            int last = pathElements.Length - 1;

            for (int i = 0; i < last; i++)
            {
                sb.Append(pathElements[i]);
                sb.Append(pathdelimiter);
            }

            sb.Append(pathElements[last]);
            sb.Append(extension);

            try
            { 
                path = Path.GetFullPath(sb.ToString());
            }
            catch (Exception e)
            {
                switch (e.GetType().Name)
                {
                    case "NotSupportedException":
                        throw new ArgumentException(@"extension or path elements misuse a colon", e.InnerException);

                    case "ArgumentNullException":
                            throw new ArgumentNullException(@"extension or path elements are null", e.InnerException);

                    case "InvalidCastException": 
                        throw new ArgumentException(@"extension or path elements contain invalid characters", e.InnerException);

                    case "SecurityException":
                        throw new ArgumentException(@"No access to that location", e.InnerException);

                    case "PathTooLongException":
                        throw new ArgumentException(@"path is too longn", e.InnerException);
                }
            }

            return path;
        }
    }
}


这篇关于如何在C#中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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